2

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:

i.e.

     @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)
1
  • 1
    That's because you are passing in the function as an argument to be unpacked and called. Functions are objects, remember that! Commented Feb 27, 2015 at 20:35

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

The command parameter takes a reference to a function -- ie: the name of the function. If you add parenthesis, you're asking python to execute the function and give the result of the function to the command parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.