您好,我想在一个子例程中创建一个倒计时,然后显示在画布上。我不完全确定从哪里开始,我已经做了一些研究,并且能够随着时间的推移做一个。sleep(x)函数,但是那个方法冻结了整个程序,这不是我想要的。我也在这里查找了关于计时器的其他问题,并试图将它们合并到我的程序中,但我还没有取得任何成功。
我想创建一个倒计时计时器,它从60秒开始倒计时,并显示在画布上,然后让它在计时器达到0时执行一些操作。
有人能指点我正确的方向吗?
提前谢谢。
编辑:有了这些建议,我试着把它们放进程序中,但运气不太好。
不确定这段代码中是否有重大错误,或者这只是一个简单的错误。 当我运行它时得到的错误低于代码。
这是我希望计时器进入的代码部分:
def main(): #First thing that loads when the program is executed.
global window
global tkinter
global canvas
global cdtimer
window = Tk()
cdtimer = 60
window.title("JailBreak Bob")
canvas = Canvas(width = 960, height = 540, bg = "white")
photo = PhotoImage(file="main.gif")
canvas.bind("<Button-1>", buttonclick_mainscreen)
canvas.pack(expand = YES, fill = BOTH)
canvas.create_image(1, 1, image = photo, anchor = NW)
window.mainloop()
def buttonclick_mainscreen(event):
pressed = ""
if event.x >18 and event.x <365 and event.y > 359 and event.y < 417 : pressed = 1
if event.x >18 and event.x <365 and event.y > 421 and event.y < 473 : pressed = 2
if event.x >18 and event.x <365 and event.y > 477 and event.y < 517 : pressed = 3
if pressed == 1 :
gamescreen()
if pressed == 2 :
helpscreen()
if pressed == 3 :
window.destroy()
def gamescreen():
photo = PhotoImage(file="gamescreen.gif")
canvas.bind("<Button-1>", buttonclick_gamescreen)
canvas.pack(expand = YES, fill = BOTH)
canvas.create_image(1, 1, image = photo, anchor = NW)
game1 = PhotoImage(file="1.gif")
canvas.create_image(30, 65, image = game1, anchor = NW)
e1 = Entry(canvas, width = 11)
e2 = Entry(canvas, width = 11)
canvas.create_window(390, 501, window=e1, anchor = NW)
canvas.create_window(551, 501, window=e2, anchor = NW)
canvas.after(1, gamescreen)
window.mainloop()
def cdtimer():
canvas.delete(ALL)
global cdtimer
cdtimer -= 1
canvas.create_text(510, 6, text=cdtimer, font="Ubuntu 29 bold", anchor = NW)
if cdtimer == 0:
scorescreen()
else:
canvas.after(1000, gamescreen)
main()
错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
return self.func(*args)
File "/usr/lib/python3.2/tkinter/__init__.py", line 490, in callit
func(*args)
File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 50, in gamescreen
e1 = Entry(canvas, width = 11)
File "/usr/lib/python3.2/tkinter/__init__.py", line 2372, in __init__
Widget.__init__(self, master, 'entry', cnf, kw)
File "/usr/lib/python3.2/tkinter/__init__.py", line 1952, in __init__
cnf = _cnfmerge((cnf, kw))
File "/usr/lib/python3.2/tkinter/__init__.py", line 71, in _cnfmerge
if isinstance(cnfs, dict):
RuntimeError: maximum recursion depth exceeded while calling a Python object
你想试试穿线吗?
只需将
sec
更改为初始值(以秒为单位),并将n
更改为要更新的时间间隔(以秒为单位)。这是奥克利答案的扩展。它演示了如何在画布中显示时间,以及如何启动整个过程:
脚本从60秒开始倒数(显示剩余的时间),当它到达0时,它调用do_something。
您想使用
after
方法。逻辑是这样的:以上将从计数器中减去一。如果计数器为零,它会做一些特殊的事情。否则,它会安排自己在一秒钟内再次运行。
相关问题 更多 >
编程相关推荐