对于捕获按键的工作方式,我可能会有根本的误解。我已经设置了基本的按键和效果,但是当我尝试在较大的脚本中执行相同的操作时,它没有效果。例如,第一个程序运行正常。我按空格键,窗口关闭。
import Tkinter as tk
class ExampleApp(tk.Tk):
def __init__(self):
def pressSpace(event):
self.destroy()
tk.Tk.__init__(self)
w, h = self.winfo_screenwidth(), self.winfo_screenheight()
self.geometry("%dx%d+0+0" % (w, h))
self.bind("<space>", pressSpace)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
但是,第二个按钮似乎在按下空格时没有任何作用。
#Program has been greatly simplified without affecting the outcome
import Tkinter as tk
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# make program fullscreen
w, h = self.winfo_screenwidth(), self.winfo_screenheight()
self.geometry("%dx%d+0+0" % (w, h))
self.label = tk.Label(self)
self.label.pack()
self.remaining = 0
self.countdown(15)
def countdown(self, remaining = None):
paused = 0
def pressSpace(event):
if(paused == 0):
paused = 1
else:
paused = 0
#bind spacebar to pause the timer
self.bind(self, "<space>", pressSpace)
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
#Time has expired. Players lose
self.label.configure(text="TIME!", fg='black', bg='brown')
else:
#There is still time on the clock. This cuts the time into readable chunks
self.label.configure(text= str(self.remaining) + ' - Paused = ' + str(paused))
if(paused == 0):
#Check if the timer is toggled to pause
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
我不明白该程序在功能上似乎相似,并且所有内容都包含在同一功能中,但是变量“ paused”似乎根本没有变化,并且计时器也不会停止。
我很害怕。我偏离了路径吗?
最佳答案
您的代码中有很多问题-
您的绑定似乎是错误的-
self.bind(self, "<space>", pressSpace)
不知道为什么再次发送
self
。你应该做的-self.bind("<space>", pressSpace)
在您的函数-
pressSpace()
中-由于您正在设置paused=0
或paused=1
,因此暂停的内容将被视为该函数的局部变量,因此会出现错误-UnboundLocalError: local variable 'paused' referenced before assignment
。相反,我会让您将paused
设置为self
上的实例变量并使用它。其次,即使绑定正确,在每次对
countdown
的调用中,您都将paused
改回0
。为此,您应该将绑定和暂停设置为0的逻辑移到__init__()
。然后在取消暂停应用程序时,您需要重新开始倒计时。有效的代码-
import Tkinter as tk
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# make program fullscreen
w, h = self.winfo_screenwidth(), self.winfo_screenheight()
self.geometry("%dx%d+0+0" % (w, h))
self.label = tk.Label(self)
self.label.pack()
self.remaining = 0
self.paused = 0
def pressSpace(event):
if(self.paused == 0):
self.paused = 1
else:
self.paused = 0
self.countdown()
#bind spacebar to pause the timer
self.bind("<space>", pressSpace)
self.countdown(15)
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
#Time has expired. Players lose
self.label.configure(text="TIME!", fg='black', bg='brown')
else:
#There is still time on the clock. This cuts the time into readable chunks
self.label.configure(text= str(self.remaining) + ' - Paused = ' + str(self.paused))
if(self.paused == 0):
#Check if the timer is toggled to pause
self.remaining = self.remaining - 1
self.after(1000, self.countdown)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
关于python - TKinter:捕获按键时无响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32546386/