我知道这是个菜鸟问题,但我想弄明白为什么"self.update_count“在从”create_widget“方法调用时不需要括号。我一直在找,但找不到原因。
请帮帮忙。
# Click Counter
# Demonstrates binding an event with an event handler
from Tkinter import *
class Skeleton(Frame):
""" GUI application which counts button clicks. """
def __init__(self, master):
""" Initialize the frame. """
Frame.__init__(self, master)
self.grid()
self.bttn_clicks = 0 # the number of button clicks
self.create_widget()
def create_widget(self):
""" Create button which displays number of clicks. """
self.bttn = Button(self)
self.bttn["text"] = "Total Clicks: 0"
# the command option invokes the method update_count() on click
self.bttn["command"] = self.update_count
self.bttn.grid()
def update_count(self):
""" Increase click count and display new total. """
self.bttn_clicks += 1
self.bttn["text"] = "Total Clicks: "+ str(self.bttn_clicks)
# main root = Tk() root.title("Click Counter") root.geometry("200x50")
app = Skeleton(root)
root.mainloop()发布于 2013-01-09 17:47:41
这不是函数调用,而是字典中的引用存储:
self.bttn["command"] = self.update_count
// stores reference to update_count inside self.bttn["command"]
// invokable by self.bttn["command"]()很可能Button对象能够在某些交互时调用此方法。
https://stackoverflow.com/questions/14232237
复制相似问题