问题描述
在下面的小GUI应用程序中.当我使用按钮的命令选项来调用函数时.它不是这样的: self.update()
,而是它是这样的: self.update
.为什么这样?按钮的命令选项是某些特殊的工作方式吗?我认为应该使用大括号()
调用方法或函数,除非它是属性:
In the little GUI app below. When I use button's command option to call a function. It doesn't work like this: self.update()
rather it works like this: self.update
. Why so? Is is some special way that command option of a button works? I think a method or a function should be called with those braces ()
, unless it's a property:
即
@name.setter:
def setter(self, name):
self.name = name
#main
object.name = "New_obj"
注意:以上只是一个模板,所以您可能会明白我的意思.我没有写完整的有效代码.包括课堂和一切.
Note: The above is just a template so you might get my point. I didn't write the complete valid code. Including class and everything.
from tkinter import *
class MuchMore(Frame):
def __init__(self, master):
super(MuchMore,self).__init__(master)
self.count =0
self.grid()
self.widgets()
def widgets(self):
self.bttn1 = Button(self, text = "OK")
self.bttn1.configure(text = "Total clicks: 0")
self.bttn1["command"] = self.update # This is what I am taking about
self.bttn1.grid()
def update(self):
self.count += 1
self.bttn1["text"] = "Total clicks" + str(self.count)
#main
root = Tk()
root.title("Much More")
root.geometry("324x454")
app = MuchMore(root)
推荐答案
这是一个高阶函数,表示您将函数作为对象引用.您不调用函数并将命令分配给函数的返回值.有关更多信息,请参见此处.
It is a high order function, meaning you are referencing a function as an object. You are not calling the function and assigning the command to the return value of the function. See here for more information.
这篇关于tkinter中的按钮命令选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!