1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import tkinter as tk
import threading
import time
done = threading.Event()
def run_thread(count=5):
while count > 0 and not done.is_set():
post_update(count)
time.sleep(0.5)
count -= 1
post_quit()
def post_quit():
app.after_idle(app.quit)
def post_update(count):
app.after_idle(lambda: display.configure(text=str(count)))
def create_display(app=None):
display = tk.Label(app, text='foo')
return display
if __name__ == '__main__':
app = tk.Tk()
display = create_display(app)
display.pack()
p = threading.Thread(target=run_thread, args=(5,))
p.start()
app.mainloop()
done.set()
p.join() |
Partager