Keyboard interrupt not working on my program

1.8k views Asked by At

I'm making a program on python 2.7 with tkinter. This is a simple tkinter GUI with just 1 button, and that button executes a function.

#Code for the tkinter

#function that the button calls
x=0
def check_entry():
    While True:
        try:
            if(x==1):
                #do something
        except KeyboardInterrupt:
            break

This is a example, in reality I'm working with a raspberry pi and what I'm checking (instead of "x") is a GPIO.input.

The problem is that KeyboardInterrupt doesn't work every time. While doing like 30 tests with the code, KeyboardInterrupt worked 2 times (after pressing CTRL-C almost 20 times).

PS1: I'm working en Raspbian.
PS2: I read that maybe the problem is with the interpreter, so I executed the program like an .exe (with chmod +x) and that didn't work.
PS3: I think the problem is that the "Try:" function executes itself many times per second, so the program doesn't catch the pressing of CTRL-C?

1

There are 1 answers

0
Roman Mindlin On

You cannot catch KeyboardInterrupt in Tcl/Tk main loop, it may be handled only by Python interpreter itself. You should use another way to interrupt you program.

Check this answer for additional info.