keyboard
keyboard copied to clipboard
"OSError: exception: access violation reading" when using Tkinter
Here is my code:
import keyboard
import tkinter as tk
from PIL import Image
def show_img(im):
window = tk.Tk()
w = h = window.winfo_screenheight() * 0.75
size = (w, h)
thumb = im.copy()
thumb.thumbnail(size, Image.ANTIALIAS)
ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
window.geometry("%dx%d+%d+%d" % (w, h, x, y))
img = ImageTk.PhotoImage(thumb)
panel = tk.Label(window, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
window.mainloop()
def on_activate():
im = Image.new("RGB", (100, 30), color=(73, 109, 137))
show_img(im)
keyboard.add_hotkey("f4", on_activate, suppress=True)
print("Listening to keyboard events")
keyboard.wait()
After displaying and closing the window generated by Tkinter, this exception occurs:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\keyboard\__init__.py", line 294, in listen
_os_keyboard.listen(self.direct_callback)
File "C:\Users\Me\AppData\Local\Programs\Python\Python39\lib\site-packages\keyboard\_winkeyboard.py", line 563, in listen
while not GetMessage(msg, 0, 0, 0):
OSError: exception: access violation reading 0x0000000000000008
Any solutions or workarounds to this issue? Maybe this: https://github.com/boppreh/keyboard/issues/256 is relevant?
after some testing, I think this error occurs when the listener detects another keyboard event while the on_activate function is still running. A possible work around to this is running any non-instantaneous operations you may have on a different thread.