How to exit from loop when any key is pressed in python?

Hi folks,

Here is the script for exit from the loop when any key is pressed. It is loop. Don’t try to put this is in while loop. It will make a infinite loop structure.

If your machine has python2.x use import Tkinter as tk. If your machine has python3.x use import tkinter as tk. This script is common for both.

try:
     import tkinter as tk
except:
     import Tkinter as tk

def main_fn():
    root = tk.Tk()
    root.bind("<Key>", key)
    root.geometry('1x1+9000+9000')
    root.mainloop()

def key(event):
    exit()

main_fn()

I feel that it may help you lot.

cheers!

 


Django-Python Web based application developer.

Share This Post

Related Articles

One Response to “How to exit from loop when any key is pressed in python?”

  1. Samsung Galaxy S4 says:

    Hi,
    Neat submit. It comes under ‘How to exit from loop when any key is pressed in python?’ is really reliable. It helps me lot. Thanks Stephen.

Leave a Reply

© 2017 Techy Diary. All rights reserved.
Powered by Charvi Groups