我试图用以下代码将文本设置到我的标签中:
import tkinter as tk
class Window(tk.Tk):
def __init__(self):
super().__init__()
self.title("Hello Tkinter")
self.label_text = tk.StringVar()
self.label_text.set("Choose One")
self.label = tk.Label(self, text=self.label_text)
self.label.pack(fill=tk.BOTH, expand=1, padx=100, pady=30)
hello_button = tk.Button(self, text="Say Hello", command=self.say_hello)
hello_button.pack(side=tk.LEFT, padx=(20, 0), pady=(0, 20))
goodbye_button = tk.Button(self, text="Say Goodbye", command=self.say_goodbye)
goodbye_button.pack(side=tk.RIGHT, padx=(0, 20), pady=(0, 20))
def say_hello(self):
self.label_text.set("Hello World")
def say_goodbye(self):
self.label_text.set("Goodbye! \n (Closing in 2 seconds)")
self.after(2000, self.destroy)
if __name__ == "__main__":
window = Window()
window.mainloop()
但我得到的结果是:
发布于 2022-10-01 15:37:23
问题是
self.label = tk.Label(self, text=self.label_text)
您需要添加像下面这样的.get():
self.label = tk.Label(self, text=self.label_text.get())
其他明智的做法是默认使用PY_VARO
https://stackoverflow.com/questions/71086246
复制相似问题