0

I have a simple Tkinter window, started from an IPython console (so mainloop() is not called, in case that's relevant)

When I press Ctrl+C in the console, nothing happens... until the Tkinter window does something, at which point it catches the KeyboardInterrupt and raises an error.

The question is: how do I catch that KeyboardInterrupt? It seems that no matter where I put a try: block, it is too late already...

Here's an example:

class App(object):
  def __init__(self,):
    self.root = tk.Tk()
    self.root.bind('<FocusIn>', self.focus_in)

  def focus_in(self, event):
    print 'I got focus'

in the IPython console:

app = App()

Now press Ctrl+C in the console. Then click on the App window... and bang:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1481, in __call__
def __call__(self, *args):
KeyboardInterrupt

Adding a 'try/except KeyboardInterrupt' block inside focus_in does not help.

So? Is this a bug? Should TKinter even see that interrupt? Can I disable SIGINT while IPython is idle? (Naturally I still want it when it is running some code...)

glep
  • 104
  • 9
  • See also the somewhat related http://stackoverflow.com/questions/31127652/cannot-catch-keyboardinterrupt-in-command-prompt-twice – gaborous Apr 22 '16 at 20:08

1 Answers1

0

One way could be to create a mainloop in another thread that's always waiting for the KeyboardInterrupt exception, although I don't know if this is a good aproach but I believe It will fix your problem.