如何使按钮文本从文本延迟更改为空名称?
How can I make the button text change, from text to an empty name with a delay?
我正在为 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()
一张图 output.So far 我想我需要导入时间库并使用 sleep() 方法来获得所需的延迟。但我仍然需要:
允许面板加载按钮(没有任何数字)
然后按钮开始依次闪烁数字,直到所有按钮都闪烁一次数字。
内存就是一切!
#!/usr/bin/python
import os
import thread,time
import random
import math
try:
dirs = os.path.dirname(os.__file__).lower()
if "python2" in dirs:
from Tkinter import *
elif "python3" in dirs:
from tkinter import *
except Exception,e : print e
LARGE_FONT = ("Verdana",20)
color = 0
root = Tk()
frame = Frame(root)
root.title("Very Hard Memory Game !")
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=8, columnspan=2)
Grid.rowconfigure(frame, 8, weight=1)
Grid.columnconfigure(frame, 0, weight=1)
class _zo : pass
zo = _zo()
zo.foo = []
zo.pick = None
zo.lq =[]
zo.a = {}
zo.level = 0
zo.lev_list = [4,9,16,25,36,49,64,81]
zo.miss = 0
def get_num(num):
h = sorted(zo.a.keys())
p = zo.foo.index(num)
if num in zo.lq and num == zo.pick :
zo.a["%0.2d_%d"%(p,num)].config(text=num, state="disabled")
if len(zo.lq) > 1 :
zo.lq.remove(num)
zo.pick = ( random.choice(zo.lq))
zo.a["my_lab"].config(text= "where is %d ?" % zo.pick)
elif len(zo.lq) == 1:
zo.lq = []
zo.foo = []
zo.pick = None
zo.level = zo.level +1
for s in zo.a.keys() :
zo.a[s].destroy()
zo.a = {}
do_cube(zo.lev_list[zo.level])
else:
zo.miss += 1
zo.a["my_lab"].config(text="Try again for (%d)!(Miss:%s)"%(zo.pick,zo.miss))
def timot(hu):
time.sleep(2)
n = sorted(zo.a.keys())
for s in n :
if not s.startswith("my"):
y = zo.foo[n.index(s)]
zo.a[s].config(text=y)
time.sleep(1)
zo.a[s].config(text="")
zo.pick = random.choice(zo.foo)
for j in n: zo.a[j].config(state="normal")
zo.a["my_lab"].config(text="where is %d ?" %zo.pick)
def do_cube(many):
bx = int(math.sqrt(many))
w = (200+(200*(zo.level*0.15)))
root.geometry("%dx%d+400+200" %(w,w+(20+(5*zo.level))))
color = "orange","red","green","blue","yellow","brown","dodgerblue","pink"
zo.foo = random.sample(range(100),many)
zo.lq = [e for e in zo.foo]
for x in range(bx):
for y in range(bx):
ind = (x*bx) + y
rand_no = zo.foo[ind]
butn = Button(frame, bg=color[x], command= lambda rand_no=rand_no:get_num(rand_no), font=LARGE_FONT,state="disabled")
butn.grid(column=x, row=y, sticky=N + S + E + W)
zo.a["%0.2d_%d"%(ind,rand_no)] = butn
for x in range(bx):
Grid.columnconfigure(frame, x, weight=1)
for y in range(bx):
Grid.rowconfigure(frame, y, weight=1)
zo.a["my_lab"] = Label(root,text="ready !")
zo.a["my_lab"].grid(column=0, row=bx+1, sticky=N + S + E + W,columnspan = bx)
thread.start_new_thread(timot,(None,))
do_cube(zo.lev_list[zo.level])
root.mainloop()
我正在为 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()
一张图 output.So far 我想我需要导入时间库并使用 sleep() 方法来获得所需的延迟。但我仍然需要:
允许面板加载按钮(没有任何数字) 然后按钮开始依次闪烁数字,直到所有按钮都闪烁一次数字。
内存就是一切!
#!/usr/bin/python
import os
import thread,time
import random
import math
try:
dirs = os.path.dirname(os.__file__).lower()
if "python2" in dirs:
from Tkinter import *
elif "python3" in dirs:
from tkinter import *
except Exception,e : print e
LARGE_FONT = ("Verdana",20)
color = 0
root = Tk()
frame = Frame(root)
root.title("Very Hard Memory Game !")
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=8, columnspan=2)
Grid.rowconfigure(frame, 8, weight=1)
Grid.columnconfigure(frame, 0, weight=1)
class _zo : pass
zo = _zo()
zo.foo = []
zo.pick = None
zo.lq =[]
zo.a = {}
zo.level = 0
zo.lev_list = [4,9,16,25,36,49,64,81]
zo.miss = 0
def get_num(num):
h = sorted(zo.a.keys())
p = zo.foo.index(num)
if num in zo.lq and num == zo.pick :
zo.a["%0.2d_%d"%(p,num)].config(text=num, state="disabled")
if len(zo.lq) > 1 :
zo.lq.remove(num)
zo.pick = ( random.choice(zo.lq))
zo.a["my_lab"].config(text= "where is %d ?" % zo.pick)
elif len(zo.lq) == 1:
zo.lq = []
zo.foo = []
zo.pick = None
zo.level = zo.level +1
for s in zo.a.keys() :
zo.a[s].destroy()
zo.a = {}
do_cube(zo.lev_list[zo.level])
else:
zo.miss += 1
zo.a["my_lab"].config(text="Try again for (%d)!(Miss:%s)"%(zo.pick,zo.miss))
def timot(hu):
time.sleep(2)
n = sorted(zo.a.keys())
for s in n :
if not s.startswith("my"):
y = zo.foo[n.index(s)]
zo.a[s].config(text=y)
time.sleep(1)
zo.a[s].config(text="")
zo.pick = random.choice(zo.foo)
for j in n: zo.a[j].config(state="normal")
zo.a["my_lab"].config(text="where is %d ?" %zo.pick)
def do_cube(many):
bx = int(math.sqrt(many))
w = (200+(200*(zo.level*0.15)))
root.geometry("%dx%d+400+200" %(w,w+(20+(5*zo.level))))
color = "orange","red","green","blue","yellow","brown","dodgerblue","pink"
zo.foo = random.sample(range(100),many)
zo.lq = [e for e in zo.foo]
for x in range(bx):
for y in range(bx):
ind = (x*bx) + y
rand_no = zo.foo[ind]
butn = Button(frame, bg=color[x], command= lambda rand_no=rand_no:get_num(rand_no), font=LARGE_FONT,state="disabled")
butn.grid(column=x, row=y, sticky=N + S + E + W)
zo.a["%0.2d_%d"%(ind,rand_no)] = butn
for x in range(bx):
Grid.columnconfigure(frame, x, weight=1)
for y in range(bx):
Grid.rowconfigure(frame, y, weight=1)
zo.a["my_lab"] = Label(root,text="ready !")
zo.a["my_lab"].grid(column=0, row=bx+1, sticky=N + S + E + W,columnspan = bx)
thread.start_new_thread(timot,(None,))
do_cube(zo.lev_list[zo.level])
root.mainloop()