after() method not working in loop in 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 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)

Related

How do I make a button not run its command 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.
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)

How do I call self in tkinter button? [duplicate]

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.

Can't tie command to button Python 3.7 [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 3 years ago.
I am trying to get a button to do some stuff in Python (using Tkinter). I want the button to get the current choice of a combobox and print it on screen.
The graphical layout is 3 separate frames, but both the button and the combobox reside in the same frame (frame #2).
The problem is that i cannot refer to the combobox. The errors i am getting read:
Frame object has no attribute 'box'
Window object has no attribute 'box'
self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command= self.wipe(), text=...)
def wipe(self):
self.box.get()
ALTERNATIVELY i tried:
def wipe(self):
self.frame2.box.get()
The goal is to simply get the selected choice from the Combobox.
HERE IS MINIMAL CODING THAT PRODUCES THE SAME ERROR:
import tkinter as tk
from tkinter import ttk
class window():
def __init__(self,root):
self.frame=tk.Frame(root)
self.key=tk.Button(self.frame,text='PRESS ME',command=self.wipe())
self.box=ttk.Combobox(self.frame, options=['1','2','3'])
self.frame.pack()
self.key.pack()
self.box.pack()
def wipe(self):
self.box.get()
master=tk.Tk()
master.geometry('400x400')
app=window(master)
master.mainloop()
I would add the tag "tkinter" to the question.
Try the following:
def wipe(self):
# code
self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command=wipe, text=...)
Notice the following:
I first defined wipe, only then used it.
I am not quite sure why you wanted to do command=self.wipe(), as there are two issues here. First, you are setting command to the result of self.wipe() which is NOT a function. Second, you haven't defined self.wipe, you defined wipe.
command=wipe Sets the command keyword argument to the
function wipe
Its been long since I dealt with tkinter, if this doesn't work, I'll try to help by checking the docs again.

Python / Tkinter - how to import files or functions without executing them first? [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 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.

Python 3.4.2 tkinter Menu Auto Calling Function [duplicate]

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)

Categories

Resources