Call a function after pressing a button (tkinter) [duplicate] - python

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
I created a button "button" and I want to call a function "function" when I press on this button.
When I run the program, the function is called and then the button appears. If I press it once again, the function is not called. So how to disable calling the function without pressing the button and how to enable calling the function at each press on the butotn?
from tkinter import *
def function():
what this function does
root = Tk()
button = Button(root,text="Call function", command=function())
button.pack()
root.mainloop()

You need to pass the function (don't call it):
button = Button(root,text="Call function", command=function)

I think you have to remove the brackets from the function, because you just want to give the function as a parameter to your button and you do not want to call the function instead.
So it will look like:
button = Button(root,text="Call function", command=function)

Related

Tkinter buttons calling functions without pressing them [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed last year.
I've had this problem for a while. When I start a program, any tkinter buttons will call their functions without being pressed or anything. and after that, I cannot use the button anymore because it only works once for some reason. Can anybody help?
This is not the actual code, but it is an example:
from tkinter import *
root = Tk()
def function():
print("activated")
button = Button(root, text="this is a button", command=function)
button.pack()
root.mainloop()
Apparently adding parentheses at the end of the command inside the button code was the problem. credit to #jasonharper for this one.

Python gui button (tkinter) function is being run when program is executed and not onclick [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
i'm currently learning python/tkinter and I have two buttons working correctly but my final button is assigned to a function, which is supposed to run on click but it instead runs when the program is executed and then will not run when the button is clicked. I have looked at a couple of other threads and cannot work out why mine wont work.
Thanks a lot
def onclickSelectSheet():
test()
buttonSelectSheet = Button(topFrame, text="Select an information sheet", command=onclickSelectSheet(), bg = '#CDCDCD')
buttonSelectSheet.place(relx=0.2, rely=0.1, relwidth=0.6, relheight=0.4)
To execute a Button-command on click, you need to set the command option as follows:
Button(..., command=lambda: onclickSelectSheet(),...) # with parenthesis
OR
Button(..., command=onclickSelectSheet,...) # without parenthesis
Using parenthesis () without lambda executes the function as soon as the Button widget is defined

How to execute python tkinter button without clicking mouse?

I build a GUI application, there is some button and they perform some task. Now i want to perform that mouse click with out clicking that mouse. like with the help of any int number. like if i put a=1 than button 1 will clicked if i put b=2 than button 2 will clicked. How can i do that?
To press the button use its invoke method.
button.invoke()
In addition to Robert's answer of using the invoke method, you can also directly call the button's function.
Assume the button is defined as follows:
button = Button(master, text="Hello World", command=callback)
Then, you can just call the function callback with callback().

Module's function auto running? [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 6 years ago.
I can't figure out why the function that I've called auto runs when I run the script, without pressing the button.
import tkinter
from tkinter import filedialog
root = tkinter.Tk ()
root.title("fool")
root.geometry("300x300")
br = tkinter.Button(root, text ="Carica File", command = filedialog.askopenfile(mode="r"))
br.pack()
Right now, you're passing the result of the call
filedialog.askopenfile(mode="r")
to the command parameter. To be able to get this result, the function is executed and you're seeing the dialog right away. What you probably want to do is just provide the name of a function to call when the button is pressed, so you could define one as
def foo():
filedialog.askopenfile(mode="r")
and use
command = foo
In the Button call. What you're doing in your code above corresponds to command = foo() instead (which executes the function), and not command = foo.
If you want to do everything in the same line, and not define an extra function, you could also use a lambda and write:
command = lambda: filedialog.askopenfile(mode="r")

Tkinter Function attached to Button executed immediately [duplicate]

This question already has answers here:
How can I pass arguments to Tkinter button's callback command?
(2 answers)
Closed 6 years ago.
What I need is to attach a function to a button that is called with a parameter. When I write the code as below however, the code is executed once when the button is created and then no more. Also, the code works fine if I get rid of the parameter and parentheses when I declare the function as an attribute of the button. How can I call the function with a parameter only when the button is pressed?
from Tkinter import *
root =Tk()
def function(parameter):
print parameter
button = Button(root, text="Button", function=function('Test'))
button.pack()
root.mainloop()
The solution is to pass the function as a lambda:
from Tkinter import *
root =Tk()
def callback(parameter):
print parameter
button = Button(root, text="Button", command=lambda: callback(1))
button.pack()
root.mainloop()
Also, as #nbro already correctly pointed out, the button attribute is command, not function.

Categories