1

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.

mac
  • 42,153
  • 26
  • 121
  • 131
AquaRegia
  • 11
  • 1
  • Wild guess: The hook procedure and Tkinter's event handling do not get along well. Any reason to not simply use Tkinters bind to handle mouse events http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm? – schlenk Jul 20 '11 at 23:10
  • Then you would have to bind the mouse to a specific widget, right? I want to detect mouse clicks in all applications, I only use Tkinter to display the information. – AquaRegia Jul 21 '11 at 00:03

0 Answers0