Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter keyboard interrupt isn't handled until tkinter frame is raised

I have a GUI application written with python+tkinter. In my workflow, I generally start the gui from the commandline, do some things in the gui and then I find myself navigating to other terminal windows to do some work. Inevitably, I want to shut down the GUI at some point, and out of habit I often just navigate to the terminal that started the GUI and send a KeyboardInterrupt (Ctrl-c). However, This interrupt is not recieved until I raise the GUI window in the Window manager. Does anyone know why this happens? If the gui is started in a single function, is there a simple workaround -- multiprocessing maybe?

like image 270
mgilson Avatar asked Apr 03 '12 17:04

mgilson


People also ask

How to handle Keyboard interrupt 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.

How can I tell if tkinter Mainloop is running?

Show activity on this post. if __name__ == "__main__": app_name = 'chrome' if is_running(name=app_name): if kill(name=app_name): print(f'{app_name} killed! ') else: print(f'{app_name} is not running!

What does Keyboard interrupt mean in python?

The KeyboardInterrupt error occurs when a user manually tries to halt the running program by using the Ctrl + C or Ctrl + Z commands or by interrupting the kernel in the case of Jupyter Notebook. To prevent the unintended use of KeyboardInterrupt that often occurs, we can use exception handling in Python.

How does root mainloop() work?

root. mainloop() is a method on the main window which we execute when we want to run our application. This method will loop forever, waiting for events from the user, until the user exits the program – either by closing the window, or by terminating the program with a keyboard interrupt in the console.


1 Answers

from the newsgroups:

I'm using Python 1.5 under Redhat Linux 5.0. I'm trying to figure out the best way to trap a SIGINT (or Ctrl-C) when using tkinter. To illustrate the problem I have, do the following ...

-- Build Python-1.5 with tkinter enabled.

-- Go into the Demo/tkinter/guido directory under the Python-1.5 build tree.

-- Type "python imageview.py image-file", where "image-file" is the full pathname of a displayable image.

-- Once the image pops up, make sure that the window focus is held by the xterm window from which the "python ..." command was just now invoked.

-- Hit Ctrl-C.

At this point, nothing happens. The Ctrl-C seems to be ignored. But now ...

-- Without hitting any more keys on the keyboard, set the window focus to the displayed image window.

As soon as that window gets the focus, the Ctrl-C takes effect.

My question is this: is there any way to restructure the "imageview.py" program so that it will respond to SIGINT (Ctrl-C) immediately, without having to set the window focus to the displayed image first?

Thanks in advance for any help you folks can give me.
---- What you're seeing is caused by the way signal handlers are handled. You're stuck in the Tcl/Tk main loop, and signal handlers are only handled by the Python interpreter. A quick workaround is to use after() to schedule a dummy function to be called once a second or so -- this will make it appear that your signal is handled in a timely manner.

--Guido van Rossum

like image 96
noob oddy Avatar answered Sep 22 '22 13:09

noob oddy