Tkinter keyboard interrupt is not handled until tkinter frame is raised

I have a GUI application written with python + tkinter. In my workflow, I usually run the gui from the command line, do some things in the gui, and then I am in other terminal windows to do some work. Inevitably, I want to disable the GUI at some point, and out of habit I often just go to the terminal, which launched the GUI and sent KeyboardInterrupt (Ctrl-c). However, this interrupt is not received until I close the GUI window in the window manager. does anyone know why this happened? If the gui runs in one function, is there an easy way - multiprocessing

maybe?

+3


source to share


1 answer


from newsgroups:

I am using Python 1.5 under Redhat Linux 5.0. I am trying to figure out the best way to capture SIGINT (or Ctrl-C) while using tkinter. to illustrate the problem I am having, do the following ...

- Build Python-1.5 with tkinter included.

- Enter Demo / tkinter / guido directory under Python-1.5 build tree.

- Enter "python image file imageview.py", where "image-file" is the full path to the displayed image.

- After the image appears, make sure the window focus is held by the xterm window from which the "python ..." command is just being invoked.

- Hit Ctrl-C.

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



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

As soon as this window gains focus, Ctrl-C takes effect.

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

Thanks in advance for the help you can give me.


What you see is caused by the processing of signal handlers. You are stuck in the main Tcl / Tk loop and signal handlers are only processed 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 mean your signal is handled in a timely manner.

- Guido van Rossum

+4


source







All Articles