pythonttkinter应用程序多行上的相同文本

2024-04-25 17:31:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,我决定尝试制作一些东西来测试它们是如何工作的。但是我找不到一个方法让一个tkinter按钮上的文本代替两行。代码如下:

import tkinter as tk
hoho = 0
def lul():
    global  hoho
    hoho = hoho + 1
    print(hoho)
class Application(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()


def createWidgets(self):
    self.hi_there = tk.Button(self, fg="green")
    self.hi_there["text"] = "Pressing buttons is fun, isn't it?"
    self.hi_there["command"] = self.lel
    self.hi_there.pack(side="top")

    def lel(self):
        lul()
root = tk.Tk()
app = Application(master=root)
app.mainloop()

如果你有办法请告诉我!你知道吗


Tags: selfmasterapplicationinittkinterdefhiframe
1条回答
网友
1楼 · 发布于 2024-04-25 17:31:47

我希望我正确理解了这个问题,但是您可以在字符串中使用\n将文本打印在按钮的新行上。你知道吗

import tkinter as tk
hoho = 0

def lul():
    global  hoho
    hoho = hoho + 1
    print(hoho)


class Application(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()


    def createWidgets(self):
        self.hi_there = tk.Button(self, fg="green")
        self.hi_there["text"] = "Pressing buttons\n is fun, isn't it?"
        self.hi_there["command"] = self.lel
        self.hi_there.pack(side="top")

    def lel(self):
        lul()


root = tk.Tk()
app = Application(master=root)
app.mainloop()

相关问题 更多 >