我一直在尝试制作一个简单的GUI,并且一直在使用tkinter的各种功能。然而,在我的一生中,我无法弄明白为什么这不起作用。
from tkinter import Tk, Label, Button, Radiobutton,IntVar,StringVar
class TestGUI:
def __init__(self, master):
self.master = master
master.title("Test GUI")
self.mode = IntVar()
self.modetext = StringVar()
self.modetext.set("What does this do?")
self.modelabel = Label(master,textvariable=self.modetext)
self.modelabel.pack()
self.close_button = Button(master, text="Close", command=master.destroy)
self.close_button.pack()
R1 = Radiobutton(master, text="Mode 1", variable=self.mode, value=0, command=self.modeset)
R1.pack()
R2 = Radiobutton(master, text="Mode 2", variable=self.mode, value=1, command=self.modeset)
R2.pack()
R3 = Radiobutton(master, text="Mode 3", variable=self.mode, value=2, command=self.modeset)
R3.pack()
def modeset(self):
self.modetext.set("Mode is " + str(self.mode.get()))
print(self.mode.get())
root = Tk()
T_GUI = TestGUI(root)
root.mainloop()据我所知,它应该做的是显示三个设置模式值的单选按钮,并在标签中显示“模式是模式”,并在选择一个模式时打印模式的值。
相反,标签永远不会显示,选择单选按钮不会改变模式的值。
有人能告诉我发生了什么事吗?
发布于 2017-02-20 09:32:07
看看你的解释,StringVar和IntVar在这里引起了问题。
在它们上指定master应该可以解决您的问题。
self.mode = IntVar(master=self.master)
self.modetext = StringVar(master=self.master)与Pyzo有关,可能是因为在大多数IDE中,省略master不会引起任何问题。
https://stackoverflow.com/questions/42339815
复制相似问题