代码之家  ›  专栏  ›  技术社区  ›  anthony

TKinter:捕获按键时无响应

  •  1
  • anthony  · 技术社区  · 9 年前

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

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

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

    我很害怕。我是否偏离了正道?

    1 回复  |  直到 9 年前
        1
  •  2
  •   Anand S Kumar    9 年前

    代码中存在大量问题-

    1. 你的装订似乎不对-

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

      不确定为什么要发送 self 再一次你应该这样做-

      self.bind("<space>", pressSpace)
      
    2. 在您的功能中- pressSpace() -因为你正在设置 paused=0 paused=1 ,paused被视为该函数的局部变量,因此您将得到一个错误- UnboundLocalError: local variable 'paused' referenced before assignment 。相反,我会让你设置 paused 作为上的实例变量 自己 并使用它。

    3. 其次,即使绑定变得正确,在每次调用 countdown ,你正在改变 暂停 返回到 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()