在 KDE Python 应用程序中处理键盘中断?
-
20-09-2019 - |
题
我正在开发 PyKDE4/PyQt4 应用程序, 自动键, ,我注意到当我向程序发送 CTRL+C 时,直到我通过 ie 与应用程序交互时才会处理键盘中断。单击菜单项或更改复选框。
lfaraone@stone:~$ /usr/bin/autokey
^C^C^C
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/autokey/ui/popupmenu.py", line 113, in on_triggered
def on_triggered(self):
KeyboardInterrupt
^C^C^C
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/autokey/ui/configwindow.py", line 423, in mousePressEvent
def mousePressEvent(self, event):
KeyboardInterrupt
尽管 /usr/bin/autokey 中有以下内容:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from autokey.autokey import Application
a = Application()
try:
a.main()
except KeyboardInterrupt:
a.shutdown()
sys.exit(0)
为什么键盘中断没有被捕获:
- 当我发出它时,而不是当我下次在 GUI 中执行操作时
- 通过最初的 try/ except 子句?
使用 Python 2.6 运行 Ubuntu 9.04。
解决方案
尝试这样做:
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
在调用之前 a.main()
.
更新: 请记住,Ctrl-C 可用于 GUI 应用程序中的复制。在Qt中最好使用Ctrl+\,这将导致事件循环终止并关闭应用程序。
不隶属于 StackOverflow