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.
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 10 months ago.
from tkinter.ttk import *
import time
root= Tk()
root.title("THis is our other lessons")
root.geometry("600x400")
prog=Progressbar(orient=HORIZONTAL,length=100,mode='determinate')
def bar():
for i in range(1,100,1):
prog['value']=i
root.update_idletasks()
root.after(2000,root.destroy)
prog.pack()
butt=Button(text="Start",command= bar())
butt.pack()
root.mainloop()
I tried this method to run the progress bar but it did not worked with time.sleep, so I tried after method but the output directly reaches to end.
butt=Button(text="Start",command= bar())
The problem is that you call bar() and take the return value as the button's command. This causes bar to execute immediately instead of when the user clicks the button. To fix this, remove the parentheses to make bar itself the command:
butt=Button(text="Start",command= bar)
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))
This question already has an answer here:
Tkinter assign button command in a for loop with lambda [duplicate]
(1 answer)
Closed 2 years ago.
Here is my code:
buttons=[]
for i in range(100):
buttons.append(Button(parent,text="0",command=lambda:[change(i)])
def change(i):
buttons[i]["text"]="1"
but as finally, the i will go to 99, I could only change the last button no matter which button I clicked. So I wonder is there any good way to do so?
The solution is very easy
Just change the command by below command
command = lambda i=i:change(i)
This will do the job for you.
Your solution not worked as expected because
python passes the last value of i
And hence to resolve this problem
The above command is the solution.
Here lambda is given argument(that is i) at the same time when each execution of loop is going and then that i
is passed to change function which is why it worked
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 tried to create a window with a button that creates another window.
m = Tk()
def new(a,b):
r = Tk()
r.geometry(str(a) + "x" + str(b) + "+0+0")
b = Button(m, text="Click", command=new(100,300)).place(x=0,y=0)
m.mainloop()
Instead of getting a window with a button i get two without clicking the button.
The two windows.png
What did i do wrong?
You're calling new as you construct the Button (technically, before you construct the Button since new must finish running so its return value can be passed as the command argument), not passing it as a callback to invoke on click.
You need to pass a (no argument) callable as the command without calling it, e.g. using a lambda to wrap your new call and thereby defer it until the lambda is called:
b = Button(m, text="Click", command=lambda: new(100,300)).place(x=0,y=0)
Inside of your Button call you're called the new function. That function is creating a new instance of Tk. This is why you have two windows opening.
Assuming you want to run the geometry operation on the first Tk instance, just pass the Tk object into your new function.
You can do it like so:
from tkinter import *
m = Tk()
def new(a, b, r):
r.geometry(str(a) + "x" + str(b) + "+0+0")
b = Button(m, text="Click", command=new(100, 300, m)).place(x=0, y=0)
m.mainloop()
This question already has answers here:
All tkinter functions run when program starts
(2 answers)
Closed 7 years ago.
I'm very new to python's tkinter gui and I am trying to use it to build a basic test.
I created the menu where one of the menu items must call a function although when I run the program I can see the output from the function before the menu item has been clicked and when the menu item is clicked it does not call the function.
My code is as follows
from tkinter import *
class cl_main():
def __init__(self, master):
lo_mainmenu = Menu(master)
lo_mainmenu.option_add('*tearOff', FALSE)
master.config(menu=lo_mainmenu)
lo_menugroup = Menu(lo_mainmenu)
lo_mainmenu.add_cascade(label="MenuGroup")
lo_menugroup.add_command(label="Command", command=f_message())
def f_message():
print ("This Function Has Been Called")
root = Tk()
co_main = cl_main(root)
root.mainloop()
I can't see what is wrong with it but I'm sure there is something horribly wrong here
lo_menugroup.add_command(label="Command", command=f_message())
callbacks shouldn't have parentheses. As it is, f_message is called right away and its return value is assigned to command, rather than the function object itself.
lo_menugroup.add_command(label="Command", command=f_message)