I am creating a reminder application in python using the Tkinter module. I need to cancel to call the function when the user clicks on the cancel remind button. I tried to assign the time (time variable that contains the time in milliseconds when the function will call) variable to 0, but it does not work.
sorry,for late respond i was creating small example this is the smallest example i can create.
code:
# example:
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button
time=10000
# creating tkinter window
root = Tk()
def function_to_cancel():
global time
time=0 # not works
button = Button(root, text = 'Remind Me! after 10 seconds')
button.pack(side = TOP, pady = 5)
cancel=Button(root,text='Cancel Remind',command=function_to_cancel)#this button will cancel the remind
cancel.pack()
print('Running...')
root.after(time, root.destroy)
mainloop()
If you understand the question, please answer.
You need to save the task ID returned by .after() and then use the ID with .after_cancel() to cancel the scheduled task:
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button
time=10000
# creating tkinter window
root = Tk()
def function_to_cancel():
#global time
#time=0 # not works
root.after_cancel(after_id)
button = Button(root, text = 'Remind Me! after 10 seconds')
button.pack(side = TOP, pady = 5)
cancel=Button(root,text='Cancel',command=function_to_cancel)#this button will cancel the remind
cancel.pack()
print('Running...')
# save the ID returned by after()
after_id = root.after(time, root.destroy)
mainloop()
Related
I am trying to make a tkinter frame that will contain an entry field and a submit button. When the submit button is pressed, I want to pass the entry string to the program and destroy the frame. After many experiments, I came up with this script:
from tkinter import *
from tkinter import ttk
import time
root = Tk()
entryframe = ttk.Frame(root)
entryframe.pack()
par = StringVar('')
entrypar = ttk.Entry(entryframe, textvariable=par)
entrypar.pack()
submit = ttk.Button(entryframe, text='Submit', command=entryframe.quit)
submit.pack()
entryframe.mainloop()
entryframe.destroy()
parval = par.get()
print(parval)
time.sleep(3)
root.mainloop()
When the "Submit" button is pressed, the parameter value is passed correctly to the script and printed. However, the entry frame is destroyed only after 3 seconds (set by the time.sleep function).
I want to destroy the entry frame immediately.
I have a slightly different version of the script in which the entry frame does get destroyed immediately (although the button itself is not destroyed), but the value of par is not printed:
from tkinter import *
from tkinter import ttk
import time
root = Tk()
entryframe = ttk.Frame(root)
entryframe.pack()
par = StringVar('')
entrypar = ttk.Entry(entryframe, textvariable=par)
entrypar.pack()
submit = ttk.Button(root, text='Submit', command=entryframe.destroy)
submit.pack()
entryframe.mainloop()
# entryframe.destroy()
parval = par.get()
print(parval)
time.sleep(3)
root.mainloop()
How can I get both actions, namely the entry frame destroyed immediately and the value of par printed?
Note 100% sure what you are trying to do but look at this code:
from tkinter import *
from tkinter import ttk
def print_results():
global user_input # If you want to access the user's input from outside the function
# Handle the user's input
user_input = entrypar.get()
print(user_input)
# Destroy whatever you want here:
entrypar.destroy()
submit.destroy()
# If you want you can also destroy the window: root.destroy()
# I will create a new `Label` with the user's input:
label = Label(root, text=user_input)
label.pack()
# Create a tkitner window
root = Tk()
# Create the entry
entrypar = ttk.Entry(root)
entrypar.pack()
# Create the button and tell tkinter to call `print_results` whenever
# the button is pressed
submit = ttk.Button(root, text="Submit", command=print_results)
submit.pack()
# Run tkinter's main loop
# It will stop only when all tkinter windows are closed
root.mainloop()
# Because of the `global user_input` now we can use:
print("Again, user_input =", user_input)
I defined a function which will destroy the entry and the button. It also creates a new label that displays the user's input.
I was able to accomplish what I wanted using the wait_window method. Here is the correct script:
from tkinter import *
from tkinter import ttk
root = Tk()
entryframe = ttk.Frame(root)
entryframe.pack()
entrypar = ttk.Entry(entryframe)
entrypar.pack()
submit = ttk.Button(entryframe, text='Submit', command=entryframe.destroy)
submit.pack()
entrypar.wait_window()
parval = entrypar.get()
print(parval)
close_button = ttk.Button(root, text='Close', command=root.destroy)
close_button.pack()
root.mainloop()
My intention was not fully apparent in my original question, and I apologize for that. Anyway, the answers did put me on the right track, and I am immensely thankful.
I have a Python code that runs query from SQL server and gives back the output whenever the run query button is clicked in Tkinter.
The issue is sometimes the query takes about 10-20 min to finish and I wouldn't know exactly how much time did the query take to complete.
The code below is a sample of printing on screen in Tkinter.
from tkinter import *
def command(d):
print(d)
a = Tk()
b = []
for c in range(0, 5):
x = Button(a, text=c, command=lambda j=c: command(j)))
x.pack()
b.append(x)
a.mainloop()
So I am thinking of adding like a status bar to print the time when the button was clicked and record time again when the process is finished after the display of message box.
You can use datetime module to get the current time and assign a callback for the button to change it on the display like this (datetime.datetime.now() gets the current time, label widget is associated with a variable in a way that when the variable changes, that label changes as well):
import tkinter as tk
import datetime
def command():
global time
time.set(str(datetime.datetime.now()))
root = tk.Tk()
time = tk.StringVar()
time.set('0')
button = tk.Button(root, text='Print time', command=command)
button.pack()
label = tk.Label(root, textvariable=time)
label.pack()
root.mainloop()
I am making a tkinter code that uses button widget but when I press the button, it stays pushed until the function which is executed on button press is not completed. I want the button to be released immediately and execute the function.
Here is a code that shows a good example of the happening:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
print('Firing in 3')
time.sleep(3) # wait for 3 seconds
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
GUI programs are typically driven in a single thread, which is controlled by the "main loop" of the graphic toolkit in use. That is: a program usually set up the application, and pass the control to the toolkit, which runs a tight loop that answers all users (and network, file, etc...) events, and the only user code ever to run again are the callbacks coded during the setup phase.
At the same time, when your code is running in during a callback, it holds controls - which means the toolkit won't be able to answer to any events while your function does not return.
What has to be done is to write code that cooperates with the GUI toolkit - that is, create events that generate further callbacks, if you need things spaced in time. In the case of tkinter, this is achieved with the method .after of a widget: after that many milliseconds, the callable passed will be run. time.sleep, on the other hand, stops the single thread there, and the event loop does not run.
In your example, you can simply write:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
print('Firing in 3')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
You can use threading inside the function you're being blocked while pressed. This is my edit on yours:
from tkinter import *
import time
import threading
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
def callback2():
print('Firing in 3')
time.sleep(3) # wait for 3 seconds
threading.Thread(target=callback2).start()
def realcallback():
print('Firing now')
def main(): # function 'main'
b = Button(
root,
text="Fire",
width=10,
height=2,
command=callback
) # setting the button
b["background"] = 'red' # button color will be red
# button color will be yellow for the time when the button will not be released
b["activebackground"] = 'yellow'
b.place(x=25, y=25) # placing the button
main() # using function 'main'
mainloop()
I would like to add:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
root.update() #<- this works for me..
print('Firing in 3')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
I'm programming some drives with python using Tkinter as GUI. When my machine is running, I'd like to show the user a toplevel window with some information which should close itself after the function completes. This is my minimal example:
from Tkinter import *
import time
def button_1():
window = Toplevel()
window.title("info")
msg = Message(window, text='running...', width=200)
msg.pack()
time.sleep(5.0)
window.destroy()
master = Tk()
frame = Frame(width=500,height=300)
frame.grid()
button_one = Button(frame, text ="Button 1", command = button_1)
button_one.grid(row = 0, column = 0, sticky = W + E)
mainloop()
The main problem is, that the toplevel window just appears after 5 seconds are over. Any suggestions?
Thanks!
time.sleep(5) is launched before the GUI has time to update, that's why the toplevel only appears after the 5 seconds are over. To correct this, you can add window.update_idletasks() before time.sleep(5) to force the update the display.
But, as Bryan Oakley points out in his answer, the GUI is frozen while time.sleep(5) is executed. I guess that your ultimate goal is not to execute time.sleep but some time consuming operation. So, if you do not want to freeze the GUI but do not know how long the execution will take, you can execute your function in a separated thread and check regularly whether it is finished using after:
import Tkinter as tk
import time
import multiprocessing
def function():
time.sleep(5)
def button_1():
window = tk.Toplevel(master)
window.title("info")
msg = tk.Message(window, text='running...', width=200)
msg.pack()
thread = multiprocessing.Process(target=function)
thread.start()
window.after(1000, check_if_running, thread, window)
def check_if_running(thread, window):
"""Check every second if the function is finished."""
if thread.is_alive():
window.after(1000, check_if_running, thread, window)
else:
window.destroy()
master = tk.Tk()
frame = tk.Frame(width=500,height=300)
frame.grid()
button_one = tk.Button(frame, text ="Launch", command=button_1)
button_one.grid(row = 0, column = 0, sticky = "we")
master.mainloop()
A general rule of thumb is that you should never call sleep in the thread that the GUI is running in. The reason is that sleep does exactly what it says, it puts the whole program to sleep. That means that it is unable to refresh the window or react to any events.
If you want to do something after a period of time, the correct way to do that is with after. For example, this will destroy the window after five seconds:
window.after(5000, window.destroy)
I am trying to make a GUI in Tkinter and am wondering how to refresh a window, namely if I fill in a rectangle, I want the GUI to delete it a specified time later. How would I go about doing this? Documentation on Tkinter seems to be thin...
Each Tkinter widget has a after method, which you can use to call your rectangle delete function e.g. in the example below first I change a msg using after, and then destruct the window using after
from Tkinter import *
def changeMsg():
label.configure(text="I will self destruct in 2 secs")
label.after(2000, root.destroy)
root = Tk()
mainContainer = Frame(root)
label = Label(mainContainer, text="")
label.configure(text="msg will change in 3 secs")
label.pack(side=LEFT, ipadx=5, ipady=5)
mainContainer.pack()
label.after(3000, changeMsg)
root.title("Timed event")
root.mainloop()