Python tkinter - 计时器

Python tkinter - timer

我需要一些帮助!我正在学习 tkinter,我决定制作游戏。我做了一个按钮和一个标签,所以当你点击按钮时,它会在屏幕上随机移动,标签会计算你的点击次数。现在,我需要一个计时器,但我不知道该怎么做。游戏应该是这样的:https://scratch.mit.edu/projects/2245518/

这是我的代码:

    from tkinter import *
from random import randint
import 

root = Tk()
root.geometry('600x470')
root.title('Catch The Button - Game')

clicks = 0

def change():
    global clicks
    clicks += 1
    clicksLabel['text'] = 'Clicks: ' + str(clicks) + 180 * ' '
    rand = randint(1,5)

    if rand == 1:
        button.pack(side = LEFT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 2:
        button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 3:
        button.pack(side = TOP, padx = randint(1, 220), pady = randint(1, 220))
    else:
        button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))


clicksLabel = Label(root, text = 'Clicks: 0' + 180 * ' ')
clicksLabel.pack()

button = Button(root, text = 'Catch me  :P', command = change)
button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))

root.mainloop()

我用以下代码做到了:

from Tkinter import * # if using python 3.x
from tkinter import * # if using python 2.x
from random import randint
import time

root = Tk()
root.geometry('600x470')
root.title('Catch The Button - Game')

t0 = time.time()
ctime = 0
clicks = 0

def change():
    global clicks
    global ctime
    global t0

    t1 = time.time()
    dt = t1 - t0
    t0 = t1

    ctime += dt
    clicks += 1

    clicksLabel['text'] = 'Clicks: ' + str(clicks) + 180 * ' '
    timeLabel['text'] = 'Time: ' + str(ctime)[:4] + 's' + 180 * ' '

    rand = randint(1,5)

    if rand == 1:
        button.pack(side = LEFT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 2:
        button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 3:
        button.pack(side = TOP, padx = randint(1, 220), pady = randint(1, 220))
    else:
        button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))


clicksLabel = Label(root, text = 'Clicks: 0' + 180 * ' ')
clicksLabel.pack()
timeLabel = Label(root, text = 'Time: 0.00 s' + 180 * ' ')
timeLabel.pack()

button = Button(root, text = 'Catch me  :P', command = change)
button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))

root.mainloop()

我简单地定义了一个开始时间和一个运行时间(分别是t0和ctime)。按下按钮时,代码

t1 = time.time()
dt = t1 - t0
t0 = t1
ctime += dt

简单地将自上次按下按钮以来的时间添加到 ctime,然后 timeLabel er 标签随新时间更新(就像 clicksLabel 更新的方式一样)。

免责声明:我没有编写一个在不单击按钮的情况下明显增加的计时器,因为这需要一个 while 循环、退出条件和不断更新,而您的程序被构造为仅在单击时更新!

希望对您有所帮助

您可以使用 after 方法让函数 运行 周期性地运行。您可以制作一个显示时间的标签,并编写一个每 100 毫秒更新一次的函数,并检查时间何时用完 运行 以显示分数:

time = 20

def update_time():
    global time
    time = time-0.1
    timeLabel.config(text=str(time))
    if time<0.01:
        timeLabel.config(text='0')
        button.pack_forget()
        Label(root, text='You scored {}'.format(clicks), font=('Helvetica', 20, 'bold')).pack()
    else:
        timeLabel.after(100, update_time)

在你的代码中(我也改变了一些其他的东西)看起来像这样

from tkinter import *
from random import randint

root = Tk()
root.geometry('600x470')
root.title('Catch The Button - Game')

clicks = 0
time = 20

def change():
    global clicks
    clicks += 1
    clicksLabel['text'] = 'Clicks: ' + str(clicks)
    rand = randint(1,5)

    if rand == 1:
        button.pack(side = LEFT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 2:
        button.pack(side = RIGHT, padx = randint(1, 220), pady = randint(1, 220))
    elif rand == 3:
        button.pack(side = TOP, padx = randint(1, 220), pady = randint(1, 220))
    else:
        button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))

def update_time():
    global time
    time = time-0.1
    timeLabel.config(text=str(time))
    if time<0.01: # Account for rounding errors
        timeLabel.config(text='0')
        button.pack_forget()
        Label(root, text='You scored {}'.format(clicks), font=('Helvetica', 20, 'bold')).pack()
    else:
        timeLabel.after(100, update_time)

clicksLabel = Label(root, text = 'Clicks: 0')
clicksLabel.pack(side=LEFT, anchor=N)

timeLabel = Label(root, text=str(time))
timeLabel.pack(side=RIGHT, anchor=N)
timeLabel.after(100, update_time)

button = Button(root, text = 'Catch me  :P', command = change)
button.pack(side = BOTTOM, padx = randint(1, 220), pady = randint(1, 220))

root.mainloop()