pyHook + Tkinter = crash?

北战南征 提交于 2019-12-13 01:11:45

问题


Consider the following example:

from Tkinter import *
import pyHook

class Main:
    def __init__(self):
        self.root = Tk()
        self.root.protocol("WM_DELETE_WINDOW", self.onClose)
        self.root.title("Timer - 000")

        self.timerView = Text(self.root, 
                    background="#000", 
                    foreground="#0C0", 
                    font=("Arial", 200), 
                    height=1, 
                    width=3)
        self.timerView.pack(fill=BOTH, expand=1)

        self.timer = 0
        self.tick()

        self.createMouseHooks()

        self.root.mainloop()

    def onClose(self):
        self.root.destroy()

    def createMouseHooks(self):
        self.mouseHook = pyHook.HookManager()
        self.mouseHook.SubscribeMouseAllButtons(self.mouseClick)
        self.mouseHook.HookMouse()

    def mouseClick(self, event):
        self.timer = 300

        return True

    def tick(self):
        self.timerView.delete(1.0, END)
        self.timerView.insert(END, self.threeDigits(self.timer))

        self.root.title("Timer - " + str(self.threeDigits(self.timer)))

        self.timer = self.timer - 1 if self.timer > 0 else 0
        self.root.after(1000, self.tick)

    def threeDigits(self, number):
        number = str(number)
        while len(number) < 3:
            number = "0" + number

        return number

if __name__ == "__main__":
    Main()

This will display a window and asynchronously update a text widget every second. It's simply a timer that will count down, and reset to 300 whenever the user clicks a mouse button.

This does work, but there's a weird bug. When the program is running, and you move the window, the mouse and program will freeze for 3-4 seconds, and then the program stops responding.

If you remove the hook or the asynchronous update, the bug won't happen.

What could be the cause of this problem?

EDIT:

I've been testing in Windows 7 with Python 2.6.

来源:https://stackoverflow.com/questions/6765362/pyhook-tkinter-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!