How can I schedule updates (f/e, to update a clock) in tkinter?
Tkinter root windows have a method called after
which can be used to schedule a function to be called after a given period of time. If that function itself calls after
you've set up an automatically recurring event.
Here is a working example:
# for python 3.x use 'tkinter' rather than 'Tkinter'
import Tkinter as tk
import time
class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="")
self.label.pack()
self.update_clock()
self.root.mainloop()
def update_clock(self):
now = time.strftime("%H:%M:%S")
self.label.configure(text=now)
self.root.after(1000, self.update_clock)
app=App()
Bear in mind that after
doesn't guarantee the function will run exactly on time. It only schedules the job to be run after a given amount of time. It the app is busy there may be a delay before it is called since Tkinter is single-threaded. The delay is typically measured in microseconds.
Making a Tkinter countdown timer update actively
I modified this How to create a timer using tkinter? to do basically what you want
# for python 3.x use 'tkinter' rather than 'Tkinter'
import Tkinter as tk
import datetime
import math
MINUTE = 60
HOUR = 60*MINUTE
class App():
def __init__(self):
self.root = tk.Tk()
self.done_time=datetime.datetime.now() + datetime.timedelta(seconds=HOUR/2) # half hour
self.label = tk.Label(text="")
self.label.pack()
self.update_clock()
self.root.mainloop()
def update_clock(self):
elapsed = self.done_time - datetime.datetime.now()
h,m,s,fractional_s = elapsed.seconds/3600,elapsed.seconds/60,elapsed.seconds%60
fractional_seconds = math.floor(elapsed.microseconds/1000000.0*100)
self.label.configure(text="%02d:%02d:%02d.%02d"%(h,m,s))
self.root.after(1000, self.update_clock)
app=App()
(I put fractional_seconds just in case you needed them ...)
How can I schedule updates (f/e, to update a clock) in tkinter?
Tkinter root windows have a method called after
which can be used to schedule a function to be called after a given period of time. If that function itself calls after
you've set up an automatically recurring event.
Here is a working example:
# for python 3.x use 'tkinter' rather than 'Tkinter'
import Tkinter as tk
import time
class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="")
self.label.pack()
self.update_clock()
self.root.mainloop()
def update_clock(self):
now = time.strftime("%H:%M:%S")
self.label.configure(text=now)
self.root.after(1000, self.update_clock)
app=App()
Bear in mind that after
doesn't guarantee the function will run exactly on time. It only schedules the job to be run after a given amount of time. It the app is busy there may be a delay before it is called since Tkinter is single-threaded. The delay is typically measured in microseconds.
How can I schedule updates (f/e, to update a clock) in tkinter?
Tkinter root windows have a method called after
which can be used to schedule a function to be called after a given period of time. If that function itself calls after
you've set up an automatically recurring event.
Here is a working example:
# for python 3.x use 'tkinter' rather than 'Tkinter'
import Tkinter as tk
import time
class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="")
self.label.pack()
self.update_clock()
self.root.mainloop()
def update_clock(self):
now = time.strftime("%H:%M:%S")
self.label.configure(text=now)
self.root.after(1000, self.update_clock)
app=App()
Bear in mind that after
doesn't guarantee the function will run exactly on time. It only schedules the job to be run after a given amount of time. It the app is busy there may be a delay before it is called since Tkinter is single-threaded. The delay is typically measured in microseconds.
Related Topics
What Is the Eafp Principle in Python
Why Do Python Classes Inherit Object
What Is a Mixin and Why Is It Useful
Converting Unix Timestamp String to Readable Date
Finding the Source Code For Built-In Python Functions
Read Subprocess Stdout Line by Line
How to Check Which Version of Python Is Running My Script
How to Check If a Variable Exists
Performant Cartesian Product (Cross Join) With Pandas
"Large Data" Workflows Using Pandas
How to Clear the Interpreter Console
Return, Return None, and No Return At All
Multiple Assignment and Evaluation Order in Python