【发布时间】:2015-11-08 16:18:37
【问题描述】:
我正在为 GUI 使用 tkinter 库。基本上我正在使用 for 循环创建 4 个按钮,每个按钮都有一个随机数作为它们的文本。现在我在尝试实现一种允许按钮文本显示为数字然后在一秒钟左右变为空的方法时陷入困境(完成此操作后,下一个按钮将执行相同的过程)。因此,该方法将允许每个按钮闪烁它的数字并移动到下一个(直到所有按钮都闪烁一次它们的数字)。
这是我目前得到的代码
from tkinter import *
from tkinter.messagebox import showinfo
from random import randint
def set_colors(a):
if a == 0:
return "red"
elif a == 1:
return "green"
elif a == 2:
return "blue"
elif a == 3:
return "yellow"
def set_random():
random_int = 0
random_int = randint(0, 100)
return random_int
LARGE_FONT = ("Verdana",20)
color = 0
root = Tk()
frame = Frame(root)
root.title("Test")
root.geometry("200x200")
root.resizable(0, 0)
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0, sticky=N + S + E + W)
grid = Frame(frame)
grid.grid(sticky=N + S + E + W, column=0, row=7, columnspan=2)
Grid.rowconfigure(frame, 7, weight=1)
Grid.columnconfigure(frame, 0, weight=1)
for x in range(2):
for y in range(2):
rand_no = set_random()
btn = rand_no
btn = Button(frame, text=btn, bg=set_colors(color) , font=LARGE_FONT)
btn.grid(column=x, row=y, sticky=N + S + E + W)
color += 1
for x in range(2):
Grid.columnconfigure(frame, x, weight=1)
for y in range(2):
Grid.rowconfigure(frame, y, weight=1)
root.mainloop()
输出的图片。到目前为止,我认为我需要导入时间库并使用 sleep() 方法来获得所需的延迟。但我仍然需要:
允许面板加载按钮(没有任何数字) 然后按钮开始一个接一个地闪烁数字,直到所有按钮都闪烁数字一次。
【问题讨论】:
标签: python python-3.x button tkinter tk