Метод event.char() обозначения клавиш, tkinter
У меня имеется небольшой код для считывания нажатия клавиш:
import tkinter as tk
root = tk.Tk()
root.title("Sys.tem")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 500
window_height = 300
x = (screen_width // 2) - (window_width // 2)
y = (screen_height // 2) - (window_height // 2)
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
text = ""
def func1(event):
global text
text += event.char
if event.char == "Клавиша":
print(text)
text = ""
root.bind("<KeyPress>",func1)
root.mainloop()
И по нажатии клавиши Enter/End должно выполняться это:
if event.char == "Клавиша":
print(text)
text = ""
Так вот, если я пишу туда Enter или End, то никакого результата нету.
Подскажите пожалуйста, какие обозначения для этих клавиш, или где их можно найти?
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Вы пытаетесь получить специальную клавишу, такую как Return, End и т. д.,
вы можете использовать keysym, который предоставит имя клавиши.
import tkinter as tk
root = tk.Tk()
root.title("Sys.tem")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 500
window_height = 300
x = (screen_width // 2) - (window_width // 2)
y = (screen_height // 2) - (window_height // 2)
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
text = ""
def func1(event):
global text
text += event.keysym if event.keysym != '??' else event.char # !!!
# if event.char == "Клавиша":
if event.keysym == "Return" or \
event.keysym == "Enter" or \
event.keysym == "End":
print(text)
text = ""
root.bind("<KeyPress>", func1)
root.mainloop()
