Python 为什么我的按钮在单击某个函数之前调用它?
我想在按下按钮后执行我的功能。但是当我运行程序时,在我点击按钮之前,按钮会调用所有按钮的函数。当我按下按钮时,在我的输出显示后,所有按钮都不起作用 程序中的其余按钮以同样的方式工作正常Python 为什么我的按钮在单击某个函数之前调用它?,python,button,tkinter,tkinter-entry,Python,Button,Tkinter,Tkinter Entry,我想在按下按钮后执行我的功能。但是当我运行程序时,在我点击按钮之前,按钮会调用所有按钮的函数。当我按下按钮时,在我的输出显示后,所有按钮都不起作用 程序中的其余按钮以同样的方式工作正常 #all the buttons calling the same function buttonEntry(num) with different parameters button1 = Button(frame_bottom, text="1", width="20", height="6", bg=bu
#all the buttons calling the same function buttonEntry(num) with different parameters
button1 = Button(frame_bottom, text="1", width="20", height="6", bg=buttonColor, command=buttonEntry("1"))
button2 = Button(frame_bottom, text="2", width="20", height="6", bg=buttonColor, command=buttonEntry("2"))
button3 = Button(frame_bottom, text="3", width="20", height="6", bg=buttonColor, command=buttonEntry("3"))
button4 = Button(frame_bottom, text="4", width="20", height="6", bg=buttonColor, command=buttonEntry("4"))
button5 = Button(frame_bottom, text="5", width="20", height="6", bg=buttonColor, command=buttonEntry("5"))
button6 = Button(frame_bottom, text="6", width="20", height="6", bg=buttonColor, command=buttonEntry("6"))
button7 = Button(frame_bottom, text="7", width="20", height="6", bg=buttonColor, command=buttonEntry("7"))
button8 = Button(frame_bottom, text="8", width="20", height="6", bg=buttonColor, command=buttonEntry("8"))
button9 = Button(frame_bottom, text="9", width="20", height="6", bg=buttonColor, command=buttonEntry("9"))
button0 = Button(frame_bottom, text="0", width="20", height="6", bg=buttonColor, command=buttonEntry("0"))
#function which doesn't execute when button is pressed
def buttonEntry(num):
n=num
print(n)
我希望按下按钮1时显示1,按下按钮2时显示2,依此类推。但当我运行程序时,所有按钮都会同时运行其命令,并显示如下输出:
1
2
3
4
5
6
7
8
9
0
Process finished with exit code 0
之后按下的按钮不会显示任何内容。您实际上并没有将函数作为回调传递,而是传递返回值。要解决此问题,请在所有内容之前添加
lambda:
:
button1 = Button(frame_bottom, text="1", width="20", height="6", bg=buttonColor, command=lambda: buttonEntry("1"))
button2 = Button(frame_bottom, text="2", width="20", height="6", bg=buttonColor, command=lambda: buttonEntry("2"))
等等。按钮小部件将回调函数作为其最后一个参数,单击按钮时将调用该参数。但是,您正在传入
buttonEntry(“1”)
,它是None
,因为这样调用buttonEntry
函数会将名为n
的局部变量设置为num
,然后打印num
,但不返回任何内容,即None
如果希望在单击按钮时调用函数,则需要传递函数本身,而不是结果:
button1 = Button(frame_bottom, text="1", width="20", height="6", bg=buttonColor, command=buttonEntry)
当然,这样回调就不知道调用了哪个按钮,因为它们都将调用buttonEntry()
。因此,您可以创建一个将被调用的lambda函数,然后使用正确的值调用buttonEntry
函数,而不是直接提供回调函数:
button1 = Button(frame_bottom, text="1", width="20", height="6", bg=buttonColor, command=lambda: buttonEntry("1"))
如果您想了解更多有关函数、返回值和lambda的信息,请阅读更多信息。问题在于如何设置每个按钮的命令:
command=buttonEntry("1")
由于buttonEntry
是一个函数,因此在此时调用它,它将打印数字并将None
分配给命令
命令
也需要在此处调用。您需要做的是创建一个工厂,创建返回预期值的函数,然后更新按钮设置:
def buttonEntryFactory(num):
def buttonEntry():
print num
return buttonEntry
button1 = Button(frame_bottom, text="1", width="20", height="6", bg=buttonColor, command=buttonEntryFactory("1"))
现在,当您定义按钮时,它将为其创建一个包含正确值的特定buttonEntry
函数,并将该函数指定给command
。当您单击该按钮时,它将按预期调用该函数
总之:command
需要一个函数(或可调用函数)作为参数,因此如果要为命令添加自定义参数,则需要使用工厂创建包含这些参数的函数。(另一种选择是使用lambda
,但我发现工厂方法有点干净)。谢谢,我不知道会这么简单