Python gui按钮(tkinter)函数在程序执行时运行,而不是onclick
我目前正在学习python/tkinter,我有两个按钮工作正常,但我的最后一个按钮被分配给一个函数,该函数应该在单击时运行,但它会在程序执行时运行,然后在单击按钮时不会运行。我已经看了几个其他的线程,无法找出为什么我的线程不能工作 非常感谢Python gui按钮(tkinter)函数在程序执行时运行,而不是onclick,python,user-interface,button,tkinter,Python,User Interface,Button,Tkinter,我目前正在学习python/tkinter,我有两个按钮工作正常,但我的最后一个按钮被分配给一个函数,该函数应该在单击时运行,但它会在程序执行时运行,然后在单击按钮时不会运行。我已经看了几个其他的线程,无法找出为什么我的线程不能工作 非常感谢 def onclickSelectSheet(): test() buttonSelectSheet = Button(topFrame, text="Select an information sheet", command=onclickS
def onclickSelectSheet():
test()
buttonSelectSheet = Button(topFrame, text="Select an information sheet", command=onclickSelectSheet(), bg = '#CDCDCD')
buttonSelectSheet.place(relx=0.2, rely=0.1, relwidth=0.6, relheight=0.4)
要在单击时执行按钮命令,您需要设置
命令
选项,如下所示:
Button(..., command=lambda: onclickSelectSheet(),...) # with parenthesis
或
使用括号
()
而不使用lambda
在定义按钮
小部件后立即执行该功能非常感谢,简短而甜蜜的解释,完全有意义!
Button(..., command=onclickSelectSheet,...) # without parenthesis