【问题标题】:Tkinter command for button not working [duplicate]按钮的Tkinter命令不起作用[重复]
【发布时间】: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 的初学者,在开始制作完整的应用程序之前,我正试图弄清楚基础知识。在此先感谢:)

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    尝试将button = Button(root, text = "Choose", command=self.select()) 更改为button = Button(root, text = "Choose", command=self.select)。注意 self.select 后删除的括号。这样,在您按下按钮之前,该方法只会被引用而不实际执行。

    【讨论】:

    • 谢谢,这很有意义。
    • 为什么每个人都反对这个问题?从字面上救了我的命! +1
    • @Noshii , 在这种情况下如何将参数传递给函数
    【解决方案2】:

    您的主要问题是设置command=self.food() 时不需要括号:

    button = Button(root, text="Choose", command=self.select)
    

    附带说明一下,生成OptionMenu 的方式有点不寻常。您可以改用以下代码,这与您的其余代码更加一致:

    option = OptionMenu(root, self.var, *food)
    

    【讨论】:

    • 谢谢,您的回答也解决了我的问题,但是 Noshii 领先您一分钟,抱歉!感谢您更改选项菜单。
    【解决方案3】:

    tkinterbook的命令参数文档。

    (按下按钮时调用的函数或方法。回调可以是函数、绑定方法或任何其他可调用的 Python 对象。如果不使用此选项,则用户按下按钮时不会发生任何事情.)

    ****************************修改代码**************** ***************

    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)
            #use function name instead of aclling the function
            button.pack()
    
        def select(self):
            selection = "You selected the food: " + self.var.get()
            print(selection) #debug message
            self.label.config(text = selection)
    
    if __name__ == '__main__':
        root = Tk()
        app = App(root)
        root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2019-07-14
      • 1970-01-01
      • 2020-05-28
      • 2012-05-13
      • 2020-04-13
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多