I'm learning Tkinter and at the moment I'm making a dice rolling program, I have encountered a problem where Tkinter freezes, I don't want that to happen as I want the program to have a simple animation where a Label
changes from a random number between 1 - 6 and eventually stops giving you the number you rolled.
Here's my code:
import random
import time
from tkinter import *
app = Tk()
app.title("Dice")
app.geometry("200x220")
l1 = Label(app, text=0)
l1.pack()
def randomizer():
b = 0
if b < 3:
a = random.randrange(0, 7, 1)
time.sleep(0.1)
l1.config(text=a)
b += 1
b1 = Button(app, text="Get New Number", command=randomizer)
b1.pack()
app.mainloop()
after
method:
import random
from tkinter import *
app = Tk()
app.title("Dice")
app.geometry("200x220")
l1 = Label(app, text=0)
l1.pack()
def change():
a = random.randrange(1, 7, 1)
l1.config(text=a)
def time():
b = 0
if b < 30:
app.after(100, change)
b += 1
b1 = Button(app, text="Get New Number", command=time)
b1.pack()
app.mainloop()
I don't know if the way I used after is correct
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…