将命令doSomething和doSomething()传递给Tkinter Button有什么区别?

时间:2019-04-29 06:05:32

标签: python tkinter

我想知道为什么class ChanceReward { private String name; private String weight; // Getter Setter ... } class ChanceRewards { private List<ChanceReward> data; // Getter Setter ... } myBtn = Button(text="btn", command=doSomething)不同。

当我创建一个按钮并添加命令doSomehting myBtn = Button(text="btn", command=doSomething())时,我得到了预期的行为-它可以执行某些操作。

但是,当我创建按钮并添加命令myBtn = Button(text="btn", command=doSomething)doSomething()时,它会发生意外的行为-它调用myBtn = Button(text="btn", command=doSomething()),而单击按钮却无济于事。

为什么会这样?

doSoemthing()

1 个答案:

答案 0 :(得分:3)

在编写doSomething, myBtn = Button(text="btn", command=doSomething时,您将传递doSomething 功能作为参数,以便单击Button时可以调用它。在编写myBtn = Button(text="btn", command=doSomething())时,您将函数的返回值作为参数传递,这意味着Button对象不能调用doSomething函数因为它只获得返回值。

相关问题