此代码工作正常,但是当我按最小化或关闭按钮时,程序窗口会冻结一段时间。原因可能是在HookMouse中,没有它,窗口最小化和接近罚款。为什么呢?
import wx
import pyHook
class myFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'My Frame')
self.tc=wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize,
wx.TE_MULTILINE|wx.TE_NOHIDESEL|wx.TE_READONLY)
self.hm = pyHook.HookManager()
self.hm.KeyDown = self.OnKeyboardEvent
self.hm.HookKeyboard()
self.hm.MouseLeftDown=self.OnKeyboardEvent
self.hm.HookMouse()
wx.EVT_CLOSE(self, self.OnClose)
def OnGetAO(self, event):
self.tc.Value+=event.MessageName+"\n"
def OnKeyboardEvent(self, event):
wx.CallAfter(self.OnGetAO, event)
def OnClose(self, event):
del self.hm
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp(0)
frame = myFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
答案 0 :(得分:1)
这似乎可以解决您的问题:
def OnClose(self, event):
self.hm.UnhookMouse()
event.Skip()
我不知道为什么,因为doc说析构函数也解开了所有注册的钩子。