【发布时间】:2018-05-17 05:54:51
【问题描述】:
我正在尝试定期在屏幕上显示一系列数字。
我是 python 新手,所以这可能很明显,但我尝试过 .after 和 pygame.time.wait,但都没有成功。
这是代码:
from tkinter import*
from random import *
import time
my_list = []
def Create_NUM(event):
x = 0
for x in range(level + 2):
button1.destroy()
num = randint(1, 100)
my_list.append(num)
Label(root, text=num,fg="red").pack()
one.pack()
time.sleep(2)
root=Tk()
num = 0
level = 1
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
button1 = Button(bottomFrame, text="Click to start game",fg="red")
button1.bind("<Button-1>", Create_NUM)
button1.pack()
root.mainloop()
【问题讨论】:
-
不要使用
sleep()- 使用root.after(milliseconds, function_name, arguments)而不是sleep()和for循环 -
请记住,现在
bind使您的按钮仅在用鼠标单击时才起作用。您应该尝试使用command选项以获得更标准的按钮使用方式。 -
查看显示current time的示例
-
这个网站上有几十个与定时器、睡眠和
after方法相关的问题。在问这个问题之前你有没有做过任何研究?