如何解决按钮不显示计时器
我正在为我 4 岁的侄子制作一个小游戏。你将两个鼠标插入电脑并尝试按下你的按钮,我需要开始按钮变成一个计时器。问题是按钮没有显示时间。它停止响应,直到时间到,然后显示“1”。 (请记住这是未完成的)
import tkinter
from tkinter import ttk
import time
score = 0
window = tkinter.Tk()
window.geometry("300x200")
def button1():
score = score - 1
def button2():
score = score + 1
def Start():
t = 30
while t > 0:
mins,secs = divmod(t,60)
timer = '{:02d}:{:02d}'.format(mins,secs)
start.config(text=t)
print(timer,end="\r")
time.sleep(1)
t -= 1
start = ttk.Button( window,text = "Start",command = Start)
start.pack(padx= 2,pady = 1)
one = ttk.Button( window,text = "Player 1",command = button1)
one.pack(padx= 2,pady = 1)
two = ttk.Button( window,text = "Player 2",command = button2)
two.pack(padx= 2,pady = 1)
window.mainloop()
解决方法
Here 是关于 tkinter 主循环的 while 循环不可消化性的答案。
您也可以通过在自己的线程中运行 start() 函数来解决此问题:
import threading
def start():
t = 30
while t > 0:
mins,secs = divmod(t,60)
timer = '{:02d}:{:02d}'.format(mins,secs)
start_button.config(text=t)
print(timer,end="\r")
time.sleep(1)
t -= 1
def start_in_thread():
thread = threading.Thread(target=start)
thread.daemon = True # forces thread to close when the tkinter mainloop is closed
thread.start()
start_button = ttk.Button(window,text="Start",command=start_in_thread)
另一个问题是您的 buttonX() 函数在没有 global 关键字的情况下更改了全局变量 score
:
def button1():
global score
score = score - 1
,
您只需删除 while
并将其替换为对 after
的调用。我还稍微清理了您的脚本,并解决了您将来会遇到的问题。像其他答案状态一样使用 threading
是矫枉过正。您只需要停止使用 mainloop
阻止 while
。
import tkinter as tk
window = tk.Tk()
window.geometry("300x200")
_PLAYTIME = 10
t,score = _PLAYTIME,0
def tally(i):
global score
score += i
start = tk.Button(window,text="Start")
start.pack(padx= 2,pady = 1)
tk.Button(window,text="Player 1",command=lambda: tally(1)).pack(padx=2,pady=1)
tk.Button(window,text="Player 2",command=lambda: tally(-1)).pack(padx=2,pady=1)
def game():
global t,score
if t > 0:
timer = '{:02d}:{:02d}'.format(*divmod(t,60))
start.config(text=f'Time: {timer} | Score: {score}')
t -= 1
start.after(1000,game)
else:
start.config(text=f'Score: {score} | Start')
t,0
start.configure(command=game)
window.mainloop()
这是完全相同的游戏,但我认为 4 岁的孩子喜欢东西...... ;)。我还添加了一个 3-2-1-Go!按开始后倒计时这样,按开始的人不会失去优势。红色和绿色矩形是您的点击区域。红色减,绿色加。
我不确定您是否为此做好了准备,但是,将 2 个鼠标插入同一台计算机不会给您 2 个指示。也许那是你游戏的一部分~为了鼠标控制而与一个 4 岁的孩子战斗:D。如果不是,您将需要 TeamPlayer 或类似的东西。或者您可以简单地使用 bind
键盘键而不是鼠标。
import tkinter as tk
root = tk.Tk()
root.geometry("800x640")
root.title('CLICKFIGHT')
#seconds to play and player handicaps
_PLAYTIME = 10
_HANDICAP1 = 0
_HANDICAP2 = 0
#time,score,countdown
t,s,cd = _PLAYTIME,3
#gameboard
board = tk.Canvas(root,highlightthickness=0,width=800,height=640)
board.pack()
#score adjuster
def tally(i):
global s
s += i
#gameboard elements
player1 = board.create_rectangle(0,400,600,fill="#ff0000")
player2 = board.create_rectangle(400,800,fill="#00ff00")
timer = board.create_text(200,50,font='Helvetica 40 bold',fill='#ffffff')
score = board.create_text(600,fill='#ffffff')
button = board.create_rectangle(0,640,fill="#0000ff",tag="button")
start = board.create_text(400,620,font='Helvetica 20 bold',fill='#ffffff',text="START",tag="button")
cdown = board.create_text(400,300,font='Helvetica 80 bold',fill='#ffffff')
#game countdown
def countdown():
global cd,p
#so you can't click start again
board.tag_unbind("button",'<1>')
if cd > 0:
board.itemconfig(cdown,text=cd)
cd-=1
root.after(800,countdown)
else:
board.itemconfig(cdown,text='GO!')
root.after(200,game)
cd = 3
#actual game
def game():
global t,s
#game stats
board.itemconfig(timer,text='Time:\t{:02d}:{:02d}'.format(*divmod(t,60)))
board.itemconfig(score,text=f'Score:\t{s}')
board.itemconfig(start,text='PLAYING...')
if t > 0:
#game looping
t -= 1
root.after(1000,game)
board.itemconfig(cdown,text='')
else:
#reset everything
t,s = _PLAYTIME,0
board.itemconfig(start,text='START')
board.itemconfig(cdown,text='GAME OVER')
board.tag_bind("button",'<1>',lambda e: countdown())
#mouse events
board.tag_bind(player1,lambda e: tally(-(1+_HANDICAP1)))
board.tag_bind(player2,lambda e: tally(1+_HANDICAP2))
board.tag_bind("button",lambda e: countdown())
root.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。