我正在研究一個PyKDE4/PyQt4應用程序Autokey,我注意到當我發送一個CTRL + C的程序時,鍵盤中斷不會被處理,直到我與應用程序交互,即按ie。點擊菜單項或更改複選框。在KDE Python應用程序中處理KeyboardInterrupt?
[email protected]:~$ /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中/自動密鑰:
#!/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)
爲什麼沒有一個KeyboardInterrupt抓到:
- 當我發出了它,而不是當我接下來通過最初的try/except子句在GUI
- 中採取行動?
使用Python 2.6運行Ubuntu 9.04。
如果sig儘快被捕獲,那麼它顯示KeyboardInterrupt沒有被提出的原因是你在Qt中的基於C的事件循環中,並且Python解釋器沒有機會註冊^ C直到事件循環的下一次。 – 2009-08-31 15:36:57
我更新了我的答案。 – 2009-08-31 21:06:42