Typing Greek characters in tkinter Typing Greek characters in tkinter tkinter tkinter

Typing Greek characters in tkinter


You can do this by binding '<Key>' (that is, the pressing of any key on the keyboard) to a function that then checks what key was pressed using event.keycode (which has the same value for any given key no matter what keyboard your computer is set to use) and then inserts the intended character. However, with multi-key keyboard shortcuts you have to keep track of previous keystrokes, because the event object passed to the function may only include the last one: even if you bind, for instance, widget.bind('<Control-Key-c>', some_function), the event passed to the function will include the c but not the Control key -- at least in the event.keycode, and the event's other attributes aren't necessarily reliable or informative when the keyboard doesn't work with tkinter. To bind /a to (lowercase alpha with a smooth breathing and accent mark), I added each '<Key>' event to a list and set the function to insert the character only if the right combination of keys was pressed:

import tkinter as tkroot=tk.Tk()e=tk.Entry(root); e.pack()keyspressed=[]def pressKey(event,widget):    keyspressed.append(event)    print(event.keycode, event.char, "pressed.")    if len(keyspressed)>1: #to avoid an index-out-of-range error if only one key has been pressed so far        #if the last two keys were / and A, insert ἄ i.e. /u1f04        if keyspressed[-2].keycode==191 and keyspressed[-2].char!='/' and event.keycode==65:            #65 is A; 191 is /; the char check is to make sure the / is not being used            # to type / on a different keyboard.            widget.insert(tk.INSERT,'\u1f04') #insert the character            return 'break' #this stops ? from being typede.bind("<Key>",lambda event: pressKey(event, e))root.mainloop()