-
Notifications
You must be signed in to change notification settings - Fork 0
/
press_key_to_continue.py
42 lines (34 loc) · 1.07 KB
/
press_key_to_continue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# copied and adapted from
# https://raw.githubusercontent.com/unfor19/mg-tools/master/mgtools/get_key_pressed.py
from tkinter import Tk, Frame
def __set_key(e, root, key_pressed):
if e.char:
key_pressed['value'] = e.char
root.destroy()
def get_key(msg="Press any key ...", time_to_sleep=3):
if msg:
print(msg)
key_pressed = {"value": ''}
root = Tk()
root.overrideredirect(True)
frame = Frame(root, width=0, height=0)
frame.bind("<KeyRelease>", lambda f: __set_key(f, root, key_pressed))
frame.pack()
root.focus_set()
frame.focus_set()
frame.focus_force() # doesn't work in a while loop without it
root.after(time_to_sleep * 1000, func=root.destroy)
try:
root.mainloop()
except KeyboardInterrupt as e:
root.destroy()
key_pressed['value'] = None
root = None # just in case
return key_pressed['value']
def __main():
c = ''
while c == '':
c = get_key("Choose your weapon ... ", 2)
print(c)
if __name__ == "__main__":
__main()