我无法使用tkinter在python中创建滚动骰子。我想创建一个使用循环滚动骰子的效果,但它不起作用,它只是冻结了一段时间,然后给出输出
from tkinter import *
import random
import time
root =Tk()
def dice():
num=["\u2680","\u2681","\u2682","\u2683","\u2684","\u2685"]
roll=f'{random.choice(num)}'
for i in range(20):
dice_label.config(text=roll)
time.sleep(0.5)
root.update()
welcome_label= Label(root, text="Welcome to Dice Roll")
welcome_label.grid(row=0,column=0)
dice_label=Label(root,font=("Helvitica",300,"bold"),text="")
dice_label.grid(row=1,column=0)
button= Button(root,text="Click to Roll",padx=50, command=dice)
button.grid(row=2,column=0)
root.mainloop()
这是带有
after
的脚本for/while
循环与mainloop
相混淆。因此,您不应该使用它们或在单独的线程上运行它们。类似地,time.sleep()
暂停执行mainloop
与@Sujay的答案相同,但没有全局变量:
相关问题 更多 >
编程相关推荐