对这段代码中一些步骤不理解
一个窗口拥有5*5按钮的划分,按钮上的数字是随机赋予的,最下面那条label是计时器,游戏规则
是从25个数字中找到最小的数字,然后点击,按钮上的数字立刻DISABLED(变成灰色),当点击
第一个按钮的同时,下面label也开始计时(按秒数算),然后找第二小的数字, 以此类推,25个数
字全部点到,看谁用时最短
提问在注释里
import tkinter as tk
import random
from random import randint
def tick():
global clock_val, after_id
if clock_started:
clock_val += 1
clock_lab["text"] = str(clock_val)
after_id = clock_lab.after(1000, tick)
def click1(event):
global clock_started
if not clock_started:#下面已经把clock_started设置成False了,not clock_started就是True,clock_started的状态一直是False,这里的if应该用不到吧
clock_started = True
clock_lab.after(1000, tick)
clicked_btn = event.widget#这条代码在这里起什么作用?
clicked_val = int(clicked_btn["text"])
if clicked_val == number[0]:
clicked_btn["state"] = tk.DISABLED
del number[0]
if len(number) == 0:
clock_started = False
clock_lab.after_cancel(after_id)
window=tk.Tk()
window.title("Clicker")
number=[]
for i in range(25):
ran_number=random.randint(1,999)
while ran_number in number:
ran_number=random.randint(1,999)
number.append(ran_number)
for i in range(25):
button_1=tk.Button(window, text=str(number[i]),width=10)
button_1.grid(row=i%5,column=i//5)
button_1.bind("<Button-1>",click1)
number.sort()
clock_val = 0
clock_lab = tk.Label(window, text=str(clock_val))
clock_lab.grid(column=2, row=5)
clock_started = False#这里为什么要设置成False
window.mainloop()