I know it’s a noob question, but I’m trying to figure out why “self.update_count”, doesn’t need parentheses when its called from the ‘create_widget’ method. I’ve been searching, but can’t find out why.
Please help.
# 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()
would be a call to the method, so
would store the result from the method in
self.bttn. However,without the parens stores the method itself in
self.bttn. In Python, methods and functions are objects that you can pass around, store in variables, etc.As a simple example of this, consider the following program: