为什么我的按钮在我点击它之前调用了一个函数?

Why does my button calls a function before I click on it?

我希望在按下按钮后执行我的功能。但是当我 运行 程序时,按钮会在我单击它们之前调用所有按钮的函数。当我按下按钮时,在我的输出显示后,none 个按钮起作用。

程序中的其他按钮以同样的方式工作正常。

#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)

我想按下button1时显示1,按下button2时显示2,所以,开。但是当我 运行 程序时,所有按钮 运行 它们的命令一次并显示如下输出:

1
2
3
4
5
6
7
8
9
0

Process finished with exit code 0

然后按下的按钮不显示任何内容。

您实际上并没有将函数作为回调传递,您传递的是 return 值。要解决此问题,请在所有内容之前添加 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"))

以此类推

Button 小部件将回调函数作为其最后一个参数,单击按钮时将调用该函数。但是,您传入的是 buttonEntry("1"),即 None,因为这样调用 buttonEntry 函数会将名为 n 的局部变量设置为 num,并且然后打印 num,但不打印 return 任何东西,即 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"))

阅读有关函数、return 值和 lambda 的更多信息,如果您想了解更多有关其工作原理的信息。

问题在于您如何为每个按钮设置命令:

command=buttonEntry("1")

因为buttonEntry是一个函数,所以此时调用它,它会打印数字并将None赋值给command。

command 也期待在这里调​​用。您需要做的是创建一个工厂来创建 return 预期值的函数,然后更新您的 Button 设置:

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,但我发现工厂方法更简洁一些)。