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 am programming with tkinter in python 2.7. Used this syntax dozens of times but for some reason in this specific occurrence after I declare the button it calls the function without me pressing it..
here is the code:
def uninstall_win():
verify = Tk()
verify.title("Uninstall")
recheck = make_entry(verify, "Please Re-enter password:", 14, show='*')
b = Button(verify, borderwidth=4, text="Uninstall", pady=8, command=uninstall(recheck.get()))
b.pack()
verify.mainloop()
def make_entry(parent, caption, width=None, **options):
Label(parent, text=caption).pack(side=TOP)
entry = Entry(parent, **options)
if width:
entry.config(width=width)
entry.pack(side=TOP, padx=10, fill=BOTH)
return entry
any insight will be appreciated
You should use lambda when putting a function with arguments in a Button.
b = Button(verify, borderwidth=4, text="Uninstall", pady=8, command=lambda: uninstall(recheck.get()))
Becuase you're calling the function instead of passing the function as a callable.
lambda: uninstall(recheck.get())
Passing uninstall(recheck.get()) is setting the command to be what this function returns
Related
This question already has answers here:
tkinter creating buttons in for loop passing command arguments
(3 answers)
Closed 1 year ago.
I'm using tkinter and this problem bothers me a lot.
My goal:
Create several buttons
When clicking a specific button, change the text shown on that button
I tried the codes below, but no matter what button I clicked, only the last one got changed.
import tkinter as tk
list1 = []
def update_btn_text(index):
list1[index].set('new')
for i in range(10):
btn_text = tk.StringVar()
list1.append(btn_text)
btn = tk.Button(root, textvariable=list1[i], command=lambda: update_btn_text(i))
btn_text.set('button' + str(i+1))
btn.pack()
root.mainloop()
I guess the problem is because of the i value, but how can I fix it?
Thanks a lot!
You have to create a local variable for every iteration and pass it as a value in updatd_btn_text()
command=lambda i=i: update_btn_text(i)
Also you could try this for configuring text of the button
def update_btn_text(index,button):
button.config(text=list1[index])
for i in range(10):
btn = tk.Button(root)
btn.pack()
btn.config (command=lambda i=i, btn=btn: update_btn_text(i, btn))
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)
How to pass arguments to a Button command in Tkinter?
(18 answers)
Closed 2 years ago.
I am trying to learn how to use tkinter and encountered a problem with buttons. What I think is happening is when I start the program it automatically calls the functions of the buttons whenever they are initialized.
code in question:
import tkinter as tk
test = tk.Tk()
x = lambda a: int((a - (a) % 3) / 3)
for i in range(9):
frame = tk.Frame(
master=test,
padx=1,
pady=1,
borderwidth=1
)
frame.grid(row=x(i), column=(i % 3))
button = tk.Button(
text=i,
master=frame,
width=10,
height=5,
bg="#333333",
fg="#ffffff",
command=print(i)
)
button.pack()
test.mainloop()
All functions with () will be executed immediately. Try to wrap your function print(i) into lambda function like that command=lambda _: print(i) and call it with command()
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.
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 7 years ago.
I have this code python, while running the code does not show any error but when Please click on the button does not show the result
from tkinter import *
root = Tk()
def cal():
l=[15,7,9,11]
s=0
for i in range(0,len(l)):
s=s+l[i]
return s
b = Button(root, text="ok", command=cal())
b.pack()
label = Label(root, text=cal())
label.pack()
root.mainloop()
You need to store a reference to the function command=cal instead of storing the function's return value command=cal().
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 7 years ago.
I want to make a button that displays a label when clicked. Here was my thinking:
from Tkinter import *
def write_hello():
w = Label(root, text = "Hello. This is a label.")
w.pack()
root = Tk()
button = Button(root, text = "Hello!", command = write_hello() )
button.pack()
root.mainloop()
Can anyone be nice enough to walk me through this? I'm very new.
You're calling the write_hello function before you even create the button, so what you're probably seeing is the label showing up before the button on the UI (and without clicking it). What you want to do is pass the function to the Button constructor instead of the function's return value:
button = Button(root, text = "Hello!", command = write_hello)