TKinter:捕获按键时无响应

TKinter: No response when capturing key presses

我可能对捕获按键的工作原理存在根本性的误解。我已经设置了一个基本的按键和一个效果,但是当我尝试在一个更大的脚本中做同样的事情时,它没有任何效果。例如,第一个程序运行良好。我按下 space 栏,然后 window 关闭。

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()

但是当按下 space 时,第二个似乎没有做任何事情。

#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' 似乎根本没有改变,计时器也没有停止。

我很害怕。我偏离了路径吗?

你的代码中有很多问题 -

  1. 你的绑定好像有误-

    self.bind(self, "<space>", pressSpace)
    

    不确定您为什么要再次发送 self。你应该做 -

    self.bind("<space>", pressSpace)
    
  2. 在你的函数中 - pressSpace() - 因为你正在设置 paused=0paused=1 ,暂停被视为该函数的局部变量,所以你会收到错误 - UnboundLocalError: local variable 'paused' referenced before assignment 。相反,我建议您将 paused 设置为 self 上的实例变量并使用它。

  3. 其次,即使绑定变得正确,在每次调用 countdown 时,您也会将 paused 改回 0。为此,您应该将绑定和设置 paused to 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()