TKinter:捕获按键时无响应

时间:2015-09-13 04:44:56

标签: python tkinter keypress

我可能对如何捕获按键操作有一个基本的误解。我已经设置了基本的按键和效果,但是当我尝试在更大的脚本中执行相同的操作时,它没有任何效果。例如,第一个程序工作得很好。我按空格键,窗口关闭。

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

我不明白。该程序似乎在功能上相似,并且所有内容都包含在同一个函数中,但变量“已暂停”。似乎根本没有改变,计时器也没有停止。

我很害怕。我是否偏离了The Path?

1 个答案:

答案 0 :(得分:2)

代码中的许多问题 -

  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。为此,您应将绑定和设置暂停移至0逻辑__init__()。然后当应用程序取消暂停时,您需要重新开始倒计时。

  4. 有效的代码 -

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