Tkinter buttons calling functions without pressing them [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 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.

Related

Tkinter Button Command switches when new Button is used [duplicate]

This question already has answers here:
tkinter creating buttons in for loop passing command arguments
(3 answers)
Closed last year.
I face an issue which is that my first button is using the second button's command. I have faced this logic error multiple times when trying to create buttons programmatically with different functions, is there a way to resolve this or is this a limitation to Tkinter? The gif below illustrates my issue.
import tkinter as tk
root = tk.Tk()
root.geometry("400x400")
def print_when_clicked(message):
print(message)
array = ["hi", "bye"]
for i in array:
tk.Button(root, text=i, command=lambda:print_when_clicked(i)).pack()
You have fallen in to one of the classic Python traps. The issue is that the lambda captures the i variable, NOT the value of i. So, after the loop finishes, i is bound to "bye", and both functions use it.
The trick to work around this is to use a fake default argument:
for i in array:
tk.Button(root, text=i, command=lambda i=i:print_when_clicked(i)).pack()

Need Help tkinker [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 trying to make a program that hides and shows all my desktop files (mac) by executing defaults write com.apple.finder CreateDesktop false #or 'true' to show my files
killall Finder
I'm using tkinker to make a gui
but everytime I execute my code, it shows all my files(without me doing anything) then when I request it to hide all my files it did, but it doesn't show them again.
Code:
import os
import tkinter as tk
def hide():
os.system("defaults write com.apple.finder CreateDesktop false")
os.system("killall Finder")
def show():
os.system("defaults write com.apple.finder CreateDesktop true")
os.system("killall Finder")
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text="Hide",
fg="red",
command=hide)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Show",
fg="blue",
command=show())
slogan.pack(side=tk.RIGHT)
root.mainloop()
Change slogan to below:
slogan = tk.Button(frame,text="Show",fg="blue",command=show)
When you say command=show(), you are calling the function and hence it gets executed without you clicking the button, so instead remove the paranthesis and it will solve the problem.
Hope it cleared your doubt, if any errors do let me know
Cheers

Problem regarding messagebox module in Tkinter [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.
My message box launches automatically without me clicking the button first when I run it in Pycharm.
from tkinter import *
from PIL import ImageTk,Image
from tkinter import messagebox
root = Tk()
root.title('Frame')
root.iconbitmap('D:\Tkinter\Frame.ico')
def popup():
messagebox.showinfo("Popup","You have clicked a button!")
Button(root, text = 'Click Me!',commmand=popup()).pack()
root.mainloop()
And this is what I get when I run it
In Button declaration, you are calling your function instead of passing a callback to it.
There is also a typo in a word 'command' - you wrote it with 3x m.
So, you should declare your button as:
Button(root, text = 'Click Me!',command=popup).pack()
How to create a basic button

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

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