Reputation: 1
im trying to build a "Key-Test" Programm which should show if a key works. (Im from the custom keyboard community)
My code is working fine for "esc" and "F1"-"F12" but with "PrintScreen" it dont work. I don't know why it works with all the other keys and not with printscreen. here is the code snippet from the relevant passage:
from tkinter import *
window = Tk()
c = Canvas(window, width=1135, height=300, bg="lavender")
c.pack()
#Create the rectangle for PrintScreen, ScrollLock and Pause
key13 = c.create_rectangle(772.5, 10, 812.5, 50, fill="white") #PrintScreen
key14 = c.create_rectangle(822.5, 10, 862.5, 50, fill="white") #ScrollLock
key15 = c.create_rectangle(872.5, 10, 912.5, 50, fill="white") # Pause
#(german) Text for each Key
text13 = c.create_text(791, 20, text="Druck", font=('Helvetica', '8'))
text14 = c.create_text(841, 20, text="Rollen", font=('Helvetica', '8'))
text15 = c.create_text(891, 20, text="Pause", font=('Helvetica', '8'))
#event functions
def key_event13(event):
c.itemconfig(key13, fill="DarkSeaGreen2")
def key_event14(event):
c.itemconfig(key14, fill="DarkSeaGreen2")
def key_event15(event):
c.itemconfig(key15, fill="DarkSeaGreen2")
#bind the keys
c.bind_all("<KeyPress-Print>", key_event13)
c.bind_all("<KeyPress-Scroll_Lock>", key_event14)
c.bind_all("<KeyPress-Pause>", key_event15)
window.mainloop()
I already tried to use
c.bind_all("event.keysym_num == 65377", key_event13)
instead of
c.bind_all("<KeyPress-Print>", key_event13)
that doesn't work either...
I hope someone here can help me. Best regards Simon
Upvotes: 0
Views: 461
Reputation: 6176
I can't tell you why exactly it doesn't work, but you can solve this by using the keyboard
module:
pip install keyboard
It has a function for listening to key presses and so you can use that to detect when print screen key is pressed and invoke a callback (so you would use this instead of bind
):
keyboard.on_press_key('print screen', key_event13)
So the complete version (without the unnecessary parts) would be:
from tkinter import Tk, Canvas
import keyboard
def key_event13(event):
# may want to add this since `keyboard` listens to all keyboard events and
# so even if the window is not in focus it will call this function
# which these two lines would prevent if needed
if window.focus_get() is None:
return
c.itemconfig(key13, fill="DarkSeaGreen2")
window = Tk()
c = Canvas(window, width=1135, height=300, bg="lavender")
c.pack()
key13 = c.create_rectangle(772.5, 10, 812.5, 50, fill="white")
text13 = c.create_text(791, 20, text="Druck", font=('Helvetica', '8'))
keyboard.on_press_key('print screen', key_event13)
window.mainloop()
Obviously for the rest of the keys just use bind
.
Also:
I strongly advise against using wildcard (*
) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2
and so on or import the whole module: import module
then You can also use an alias: import module as md
or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.
As a side note (my opinion) for organizing your code I would also suggest that you separate functions from the GUI as I have done in my example so that the code is more readable.
Upvotes: 0