Hi
I wish to write a program that will perform some action on a key press event but if there is no key press in a certain period of time say 10 seconds, it should display an error message and exit. I am working on windows and am using the Tkinter GUI. I have searched the net for a possible solution but though I have found a code which would display an error message on a mouse click event etc. I cannot understand how I can code the program to exit if no keys or mouse button is pressed.
Please could you give me some ideas as to how I should do this.
Thanks for your help
Nidhi

Maybe you could create some kind of while loop who keeps checking if a user enters something and that breaks after 10 seconds and displays an error.

while current_time < error_time   #error_time being your first time plus 10 seconds.
    if user_input :
        do_something
   else:
       current_time = time.ctime(time.time())   #update current time
display_error()  #after the while loop breaks so after 10 seconds have passed display an error.

this is just an example I hope it helps. The python time module should be helpful.
Just don't use sleep(10) cause then you would not be able to get any user input.
I'm not familiar with Tinker so I don't know in detail how to implement this but I hope it helps or gives you an idea.

If you're looking for a single key-press event, my suggestion is to use 'termios', but you'll need to make sure that you import termios before you try to run it.

Here's something that I use when waiting for a user to input a single key strong from a menu option:

def getkey():
     fd = sys.stdin.fileno()
     old = termios.tcgetattr(fd)
     new = termios.tcgetattr(fd)
     new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
     new[6][termios.VMIN] = 1
     new[6][termios.VTIME] = 0
     termios.tcsetattr(fd, termios.TCSANOW, new)
     key = None
     try:
         key = os.read(fd, 4)
     finally:
         termios.tcsetattr(fd, termios.TCSAFLUSH, old)
     return key
 
 
 # Use this list to find the key that we want.  This is hard coded for xterm.
 def chkkey(key):
     keylist = { '\x1b':'escape', '\x7f':'backspace', '\x1bOH':'HOME',
                 '\x1bOP':'F1',  '\x1bOQ':'F2',   '\x1bOR':'F3',  '\x1bOS':'F4',
                 '\x1b[15':'F5', '\x1b[17':'F6',  '\x1b[18':'F7', '\x1b[19':'F8',
                 '\x1b[20':'F9', '\x1b[21':'F10', '\x1b[23':'F11',
                 '\x1b[24':'F12',
                 '\x1b[A':'ARROW_UP', '\x1b[B':'ARROW_DN',
                 '\x1b[C':'ARROW_RT', '\x1b[D':'ARROW_LT',
                 '1':'1', '2':'2', '3':'3', '4':'4', '5':'5', '6':'6', '7':'7',
                 '8':'8', '9':'9', '0':'0' }
 
     if key in keylist:
         return keylist[key]
     else:
         return "Unknown"

That's the best way that I can suggest, but there are many other way, such as using 'curses' (a.k.a. ncurses) and I've even seen a few people suggest tkinter without loading a window.

This article has been dead for over six months. Start a new discussion instead.