在 KDE Python 应用程序中处理键盘中断?

发布于 2024-08-03 04:13:13 字数 998 浏览 4 评论 0原文

我正在开发 PyKDE4/PyQt4 应用程序 Autokey,我注意到当我向程序发送 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)

为什么没有捕获 KeyboardInterrupt:

  • 在 GUI 中采取操作时
  • 当我发出它时,而不是当我下次通过初始 try/ except 子句

?使用 Python 2.6 运行 Ubuntu 9.04。

I'm working on a PyKDE4/PyQt4 application, Autokey, and I noticed that when I send the program a CTRL+C, the keyboard interrupt is not processed until I interact with the application, by ie. clicking on a menu item or changing a checkbox.

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

This is despite having the following in /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)

Why isn't the KeyboardInterrupt caught:

  • when I issue it, rather than when I next take an action in the GUI
  • by the initial try/except clause?

Running Ubuntu 9.04 with Python 2.6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2024-08-10 04:13:13

尝试这样做:

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

在调用 a.main() 之前。

更新: 请记住,Ctrl-C 可用于 GUI 应用程序中的复制。在Qt中最好使用Ctrl+\,这将导致事件循环终止并关闭应用程序。

Try doing this:

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

before invoking a.main().

Update: Remember, Ctrl-C can be used for Copy in GUI applications. It's better to use Ctrl+\ in Qt, which will cause the event loop to terminate and the application to close.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文