I am very new to Python an tkinter. I am developing a keyboard using tkinter. I have created a text box and I am trying to bind keys to it so that users can use keyboard for input. I want to print different keys when a key is pressed. For an example, I want to print P when 'p' is pressed, A when 'a' is pressed. I know I can manually type in each key and add a function to print the required value but that would be time consuming so I decided to use a dictionary.
Below is the code for this.
def print_letter(letter):
if letter == "backspace":
text.configure(state="normal")
value = text.get("1.0", "end")
text.delete("1.0", "end")
text.insert("1.0", value[:-2])
text.configure(state="disabled")
else:
text.configure(state="normal")
text.insert("end", letter)
text.configure(state="disabled")
letters = {
'a': 'A',
'b': 'B',
'c': 'C',
'd': 'D',
'e': 'E',
}
root = tk.Tk()
root.geometry("800x415")
root.resizable(False, False)
text = tk.Text(text_frame, width=97, height=15)
text.configure(state="disabled")
text.grid(row=0, column=0, sticky="EW")
text.focus_set()
for key, value in letters.items():
text.bind(key, lambda value: print_letter(letters.get(key)))
But only the last value in the dictionary is bounded to any key I press. I saw a few posts on SO and they all suggested the below options:
- text.bind(key, lambda value=value: print_letter(letters.get(key)))
- text.bind(key, lambda args=value: print_letter(letters.get(key)))
All the above gave me the same result.
Any help is appreciated.