Typing Greek characters in tkinter

拥有回忆 提交于 2021-01-27 06:52:01

问题


I'm trying to write an interface (in Python 3.8, using tkinter) to accept text in Greek (typed using the Greek Polytonic keyboard in Windows 10). However, the Entry and Text won't accept all typed Greek characters: Greek letters by themselves can be typed, but if I try to type any letters with diacritics other than the acute accent, ? is displayed instead of the character. (I think that tkinter accepts characters in the "Greek and Coptic" but not the "Greek Extended" Unicode block.) I know that tkinter can display such characters because they show up fine when they're inserted by the program (e.g. TextInstance.insert(tkinter.INSERT, 'ῆ') inserts but just typing that character using the keyboard's shortcut inserts ?). What do I need to do for tkinter to recognize typed Greek characters?

(I also tried just re-binding the keyboard shortcuts by adding
TextInstance.bind('[h', lambda *ignored: TextInstance.insert(tkinter.INSERT, 'ῆ'))
with each character and its shortcut; that worked in the English keyboard (although the characters that activated the event were also inserted), but in the Greek Polytonic keyboard bindings on letter keys weren't activated at all.)


回答1:


γεια σου φιλε,

unicode works perfectly fine with tkinter and python

You may are also intrested in the unicode "greek + extended"


My first answer was too short to give the whole idea behind this technic: So the idea is that you transform the characters when needed.

As I remember right on the greek keyboard you press and hold alt and press a character that you want to transform, if pressed multiple times it changes again.

Here is what I've made to make it more explicit and hope it will give you the idea to make your your code work how you like it.

import tkinter as tk

root = tk.Tk()


E = tk.Entry(root)
E.pack()

_mod = tk.BooleanVar(value=False)
_val = tk.StringVar()


def tracker(event):   
    if event.keysym_num == 65513:
        _mod.set(True) #check if "left alt" key was pressed and set flag
        
    key = event.keysym #check which key was preesed
    if _mod.get() and event.char != '': #if flag(_mod is True and character is char
        if key == 'a': #if key is char a
            if not _val.get(): #if there wasn't an setting yet for _val
                a = '\u03B1' #unicode alpha
            if _val.get() == '\u03B1': #if _val.get is unicode alpha
                a = '\u03AC' #unicode alpha with tonos
            _val.set(a)
            return "break"  #dont go for default binding

def mod_off(event):
    _mod.set(False) #set flag to false
    val = _val.get() #get the chosen character
    E.insert('end', val) #insert it in entry
    _val.set('') #set _val to nothing

E.bind('<Key>',tracker)
E.bind('<KeyRelease-Alt_L>', mod_off)


root.mainloop()



回答2:


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 tk

root=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 typed

e.bind("<Key>",lambda event: pressKey(event, e))

root.mainloop()


来源:https://stackoverflow.com/questions/63542060/typing-greek-characters-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!