【发布时间】:2021-07-22 17:18:30
【问题描述】:
所以我的目标是使用单个函数在单击按钮时显示文本消息。然后应该有一个延迟,然后应该显示另一条短信。
该游戏是一个骰子游戏,单击按钮时应显示“Rolling...”。然后过了一会儿,它应该会显示一个随机数。
我尝试了 .sleep() 和 .after() 两者都导致我的程序没有显示延迟前的文本。这是我的代码:
# Imports
import tkinter as tk
from random import randrange
import time
# Global variables
# SIDES is a constant
SIDES = 12
# Functions
def func():
display["text"] = "Rolling..."
window.after(2000)
display["text"] = str(randrange(SIDES) + 1)
# Main program loop
window = tk.Tk()
display = tk.Label(window, text="Press the button \nto roll the dice.", width=20, height=3)
button = tk.Button(window, text="Roll", command=func)
display.pack()
button.pack(pady=10)
window.mainloop()
任何帮助将不胜感激!
【问题讨论】:
-
附带建议,
randint(1, SIDES)比randrange(SIDES)+1更具描述性。
标签: python function tkinter sleep