我知道這是一個noob問題,但我試圖弄清楚爲什麼「self.update_count」,在從'create_widget'方法調用它時不需要括號。我一直在尋找,但無法找出原因。Python - 爲什麼在另一個實例方法中調用這個實例方法時不需要括號?
請幫忙。
# 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()
噢,好的。謝啦。 –