Function calls with & without parentheses Function calls with & without parentheses tkinter tkinter

Function calls with & without parentheses


Best way to start and to learn what is going on here, I guess is to point out that tkinter is based on another programming language, called tcl. tkinter is a GUI package in tcl and works with python through a wrapper. So you need to understand a little of both languages here to understand what is going on.

First consider this code here:

import tkinter as tkdef func():    print('func is running')root = tk.Tk()b = tk.Button(root, text='exampel', command=func)b.pack()root.mainloop()

This is a standard script of creating a window, with a button that can be executed.If you would add a line of code before root.mainloop() that has the form of this:

print(func)

Output for me

<function func at 0x03D803D8>

this is the information that is stored in this variable. It let you know that this is a object of function with the name func and you will find it at 0x03D803D8.This information will be interpreted by the wrapper of tkinter and transform it into a string of tcl code. You can proof that by this line:

print(b['command'])

Output

31262160func

This bit of code contains simliar information like python. It contains the information that this is a function and where to find it. I wrote in some depth how Functions & lambda works in python. But what you need to know for this question is just that the syntax () executes the function. After the execution it returns to that point it was called. In python you can return things like strings; integers; objects; etc that will be get in place where the function was called.


So lets consider this code here:

import tkinter as tkdef func():    print('func is running')def get_func():    return funcroot = tk.Tk()b = tk.Button(root, text='exampel', command=get_func())b.pack()root.mainloop()

So what happens here is nonsense, but shows you how it goes. Apart from the code that we considered first we exchanged the function by the optional argument of command with the new function get_func(). Like I explained the function will be executed by the syntax () and with the statement return func we place that fnction object where we called it. So the codes looks more like this, after running:

b = tk.Button(root, text='exampel', command=func)

Add this bit of code here, somewhere over root.mainloop() to get more information of what happens here:

print(get_func)print(get_func())print('next')print(func)print(func())

Output

<function get_func at 0x02600390> #get_func function<function func at 0x046303D8> #returned by get_functionnext<function func at 0x046303D8> #func functionfunc is running None #returned by func

Let me know if some questions are left.


Update

I still get confused since you called just func and not func() viacommand.

The func is stored with the information like I already explained. The parantheses are just like a synonym for run that function. What you generally want to do is to store the function and call it when the Click_Event is happen and not to run that function while the Button is built up. So just imagin that the () is added on the stored function everytime the Click_Event is happen.

I hope you accept this as an answer. Because further more question would include much more information about Class;Eventloop and much more.


When you are using the parameter: command=button_equal you are telling the button which function to call.

When the button itself is clicked, it then knows which function to call and it calls it at that time and not sooner or later.