pyHook+pyGTK按键关闭应用程序(Windows)

2024-04-19 18:57:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望我的(Python/Windows)guigtk窗口在按键时关闭。但是,没有任何反应。 我是个初学者,我在谷歌上寻找答案。 我的英语也不是很专业。 请对我耐心点。在

import pygtk
import gtk
import pyHook

class Program:
    def QuitOnKeyPress(self):
        if pyHook.GetKeyState(81) == '1':
           gtk.main_quit()

def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.set_position(gtk.WIN_POS_CENTER)
    self.window.set_size_request(300, 300)
    self.window.show()

def main(self):
    gtk.main()


if __name__ == "__main__":
    prog = Program()
    prog.main()

while 1:
    prog.QuitOnKeyPress() #Tried without () too

你能告诉我我做错什么了吗? 我也尝试过使用win32api和pyGame。 但是win32api [from here]还没有安装,只有win32com。 PyGame也有一个问题-没有安装键盘事件/模块。在


Tags: importselfgtkifmainwindowsdefwindow
1条回答
网友
1楼 · 发布于 2024-04-19 18:57:08

检查the pyHook tutorial.检查是否按住某个键的while循环方法将无法正常工作。相反,它应该是这样的:

def OnKeyboardEvent(event):
    if event.KeyID == 81:
        gtk.main_quit()

# return True to pass the event to other handlers
    return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

相关问题 更多 >