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 1 year ago.
I have this following menu created in Tkinter
subMenu = Menu(Selection)
Selection.add_cascade(label = "Maze Generation Algorithms", menu = subMenu)
subMenu.add_command(label = "Recursive Back Tracking", command = FindNext(Stack[0], Stack, canvas, root))
The problem is when I start up my program, the FindNext function will automatically run without the menu button being pressed yet. How would I fix this?
Try to use lambda
subMenu.add_command(label = "Recursive Back Tracking", command = lambda:FindNext(Stack[0], Stack, canvas, root))
Related
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 9 months ago.
I have three dropdown menus and 3 string variables that will take on the value of the menus, However in the function that I define query, it seems as if the string variables do not take on the value each time I select a new one on the dropdown menu. It will print the set value and do nothing else. Also what's weird is that it was working perfectly and then suddenly didn't work at all.
Really need help on this one thanks.
root=Tk()
root.title('Query')
root.geometry('600x400')
def query():
print(clicked_id_mouse.get())
print(clicked_id_filename.get())
print(clicked_id_researcher.get())
clicked_id_mouse=StringVar()
clicked_id_mouse.set("Choose Mouse Id")
drop_mouse= OptionMenu(root,clicked_id_mouse,*mouse_id)
drop_mouse.place(x=50,y=150)
clicked_id_researcher=StringVar()
clicked_id_researcher.set("Choose Researcher")
researcher= OptionMenu(root,clicked_id_researcher,*r_i)
researcher.place(x=200,y=150)
clicked_id_filename=StringVar()
clicked_id_filename.set("Choose Filename")
filename= OptionMenu(root,clicked_id_filename,*filename)
filename.place(x=350,y=150)
button= Button(root,text="Query",command=query())
button.place(x=210,y=200)
root.mainloop()
When you are assigning a command to a button use "lambda: ...". Otherwise it will execute it only once the button is created. Your code should work as intended by changing
button= Button(root,text="Query",command=query())
into
button= Button(root,text="Query",command=lambda: query())
I hope that helps.
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.
When I try to add a command to a button it runs the command without me pressing the button.
How do I fix this?
For example, if I did this:
import tkinter
root = tkinter.Tk()
def a():
print("Hello")
button = tkinter.Button(root,command=a())
button.pack()
root.mainloop()
and ran it, it would execute the function a() without me pressing the button.
tkinter.Button(root,command=a())
You should pass the function, not call it, so remove the ():
tkinter.Button(root,command=a)
This question already has answers here:
tkinter creating buttons in for loop passing command arguments
(3 answers)
How to pass arguments to a Button command in Tkinter?
(18 answers)
Closed 2 years ago.
I was creating a sort of button-based paint with tkinter, so I wanted to give every button a command to paint itself.
for i in range(xc*yc):
grid.append(Button(wn,text=" ",bg="white",padx=5,pady=5))
grid[-1].config(command = paint(i)) <--????
grid[-1].place(x= (i%xc) * 30 +60, y = (math.floor(i/xc) * 30)+30)
The problem is that every button recieves the command "paint(i)" with the final value of i, so everytime it paints the last button
Use root.update() method forecefully updates UI.so you can use that to see every iteration of for loop , if you call it inside that
Maybe something like
for i in range(xc*yc):
button = Button(wn,text=" ",bg="white",padx=5,pady=5)
grid.append(button)
button.config(command = paint(i, button)) # note the additional reference to button here!
button.place(x= (i%xc) * 30 +60, y = (math.floor(i/xc) * 30)+30)
would be helpful.
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 5 years ago.
I am trying to import another file to be executed when clicking a button. So I have:
from tkinter import *
import file
window = Tk()
button = Button(window, text='GO', command=file.function())
button.grid(column=1, row=1)
This executes the file before the window is initialized. I also tried:
from file import function
button = Button(window, text='GO', command=function())
but it does the same thing. And neither of them are executed when clicking the button. How do you import files or functions but only executing them when the button is clicked? I am using python 3.5.
Thank you
You should do command=file.function instead of command=file.function().
The second one will call the function at the start of the program. In the first case, the function will be called when the button is clicked.
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 have the following lines in a large program.
username = Entry(loginPage)
password = Entry(loginPage)
button1 = Button(loginPage,text = "Login", fg = "red", command = loginClicked(username.get(),password.get()))
When the program is run, the function loginClicked runs once at the start (when the fields are empty and the button hasn't been clicked) and that is the only time it runs. After, when the button is clicked the function doesn't run at all. A print statement in the function confirms this.
As mentioned in the comments, when you create the widget you are calling ('running') the function before the widget is created, instead of passing the function handle (may be wrong terminology here) to the widget option command=.
This can be solved by using anonymous functions with lambda:
button1 = Button(root,text = "Login",
fg = "red",
command = lambda: loginClicked(username.get(), password.get()))
This creates a 'throw-away' function to feed into Tkinter's callback, which calls your function loginClicked() with its correct arguments.
You can also read effbot.org for more information on Tkinter callbacks