How Do I Make Time.sleep() Work With Tkinter?
I'm trying to show a sequence of numbers on the screen at regular intervals. I'm new to python so it may be something obvious but I have tried .after and pygame.time.wait, but nei
Solution 1:
I assume you want to show new number in place of old number, not below it.
import tkinter as tk
import random
def start():
# hide button
button.pack_forget()
# run `add_number` first time
add_number(level+2)
def add_number(x):
num = random.randint(1, 100)
my_list.append(num)
label['text'] = num
if x > 0:
# repeat after 2000ms (2s)
root.after(2000, add_number, x-1)
else:
# show button again after the end
button.pack()
# --- main ---
my_list = []
level = 1
root = tk.Tk()
label = tk.Label(root)
label.pack()
button = tk.Button(root, text="Click to start game", command=start)
button.pack()
root.mainloop()
Solution 2:
Just use this simple command in your function root.update()
Post a Comment for "How Do I Make Time.sleep() Work With Tkinter?"