How To Run Pynput.listener Simultaneously With Tkinter.tk().mainloop()
I am a teacher. I teach math, but since education is facing human resources crisis, I have some additional duties. I teach kids a bit of programming, they do quite well. Now I'd li
Solution 1:
You don't need Listener
in tkinter
. You can use root.bind
to assign function to events press and release.
from tkinter import *
def on_press(event):
#print('on_press: event:', event)
#print('on_press: keysym:', event.keysym)
print('{0} pressed'.format(event.keysym))
def on_release(event):
#print('on_release: event:', event)
#print('on_release: keysym:', event.keysym)
print('{0} release'.format(event.keysym))
if event.keysym == 'Escape':
print("exist program")
root.destroy()
root = Tk()
root.bind('<KeyPress>', on_press)
root.bind('<KeyRelease>', on_release)
root.mainloop()
You can also assign function to every key separatelly
from tkinter import *
def on_escape(event):
print("exist program")
root.destroy()
root = Tk()
root.bind('<Escape>', on_escape)
#root.bind('<KeyPress-Escape>', on_press_escape)
#root.bind('<KeyRelease-Escape>', on_release_escape)
root.mainloop()
Keysyms in Tcl/Tk documentation: https://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm
BTW:
If you want to run tkinter
and pynput
at the same time then you have to do it before join()
withListener(on_press=on_press, on_release=on_release) as listener:
root = Tk()
root.mainloop()
#listener.stop()
listener.join()
or
listener = Listener(on_press=on_press, on_release=on_release)
listener.start()
root = Tk()
root.mainloop()
#listener.stop()
listener.join()
Solution 2:
Listener
is a thread, so if you join it your main thread will wait until its end to continue processing.
You can just create a Listener
object without the with
statement and it will run along the main thread (until a callback function will return False
)
Post a Comment for "How To Run Pynput.listener Simultaneously With Tkinter.tk().mainloop()"