Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keyboard interrupt with with python gtk?

Tags:

So just like the question says, I'm trying to let keyboard interrupts happens while Gtk.main() is in progress, however, it just doesn't seem to notice that the keyboard interrupt happens until after the function is done.

So I tried sticking Gtk.main() in a separate thread, and have the main thread find the keyboard interupts, and terminate the thread, but then found out that Gtk doesn't play nicely with threads as described in this article

I can't think of any other way to let keyboard interrupts work. Is this possible?

I'm using python3, and want my program to eventually be cross platform.

like image 204
QxQ Avatar asked May 07 '13 03:05

QxQ


People also ask

How do you code a KeyboardInterrupt in Python?

In Python, there is no special syntax for the KeyboardInterrupt exception; it is handled in the usual try and except block. The code that potentially causes the problem is written inside the try block, and the 'raise' keyword is used to raise the exception, or the python interpreter raises it automatically.

Is keyboard an interrupt?

In computing, keyboard interrupt may refer to: A special case of signal (computing), a condition (often implemented as an exception) usually generated by the keyboard in the text user interface. A hardware interrupt generated when a key is pressed or released, see keyboard controller (computing)

What signal is KeyboardInterrupt?

The signal module is utilized to provide functions and mechanisms that use signal handlers in Python. We can catch the SIGINT signal, which is basically an interrupt from the keyboard Ctrl + C . Raising the KeyboardInterrupt is the default action when this happens.


2 Answers

because of https://bugzilla.gnome.org/show_bug.cgi?id=622084 gtk applications written using pygobject will not close themselves when using Ctrl + C on the terminal.

to work around it, you can install a Unix signal handler like this:

if __name__ == '__main__':     import signal     signal.signal(signal.SIGINT, signal.SIG_DFL)     your_application_main() 
like image 118
ebassi Avatar answered Sep 28 '22 13:09

ebassi


The accepted answer would not work for me. I resolved it by replacing the Gtk.main() call with GLib.MainLoop().run(), as explained in the bug report.

like image 38
sleblanc Avatar answered Sep 28 '22 13:09

sleblanc