Handling KeyboardInterrupt in a KDE Python application?

I am working on a PyKDE4 / PyQt4 application, Autokey , and I noticed that when I send a CTRL + C program, the keyboard interrupt is not handled until I interact with the application, i.e. 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 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 is KeyboardInterrupt not caught:

  • when I issue it, not when I perform an action in the GUI
  • from the original try / except clause?

Running Ubuntu 9.04 with Python 2.6.

+2


source to share


1 answer


Try this:

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

      



before calling a.main()

.

Update: . Remember that Ctrl-C can be used for copying in GUI applications. Better to use Ctrl + \ in Qt, which will terminate the event loop and close the application.

+4


source







All Articles