[英]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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.