【发布时间】:2018-01-24 09:30:34
【问题描述】:
我试图让我的程序根据下拉菜单中选择的变量更改文本,但激活命令的按钮似乎不起作用。据我所见,选择功能在程序加载后运行,然后不再运行,无论我何时单击按钮。
from Tkinter import *
class App:
def __init__(self, root):
self.title = Label(root, text="Choose a food: ",
justify = LEFT, padx = 20).pack()
self.label = Label(root, text = "Please select a food.")
self.label.pack()
self.var = StringVar()
self.var.set("Apple")
food = ["Apple", "Banana", "Pear"]
option = apply(OptionMenu, (root, self.var) + tuple(food))
option.pack()
button = Button(root, text = "Choose", command=self.select())
button.pack()
def select(self):
selection = "You selected the food: " + self.var.get()
print(self.var.get()) #debug message
self.label.config(text = selection)
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
我是 Tkinter 的初学者,在开始制作完整的应用程序之前,我正试图弄清楚基础知识。在此先感谢:)
【问题讨论】: