在Tkinter中键入希腊字符 [英] Typing Greek characters in tkinter

查看:61
本文介绍了在Tkinter中键入希腊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个接口(在Python 3.8中,使用tkinter)来接受希腊文本(在Windows 10中使用希腊多调键盘键入).但是, Entry Text 不会接受所有键入的希腊字符:可以单独键入希腊字母,但是如果我尝试使用除变音符号之外的其他变音符键入任何字母,带有重音符号,将显示?而不是字符.(我认为tkinter接受希腊语和科普特语"中的字符,但不接受希腊语扩展" Unicode块中的字符.)我知道tkinter可以显示此类字符,因为当它们被程序插入时它们会很好地显示(例如 TextInstance.insert(tkinter.INSERT,'ῆ')插入,但仅使用键盘快捷键键入该字符即可插入?).我需要做什么才能让tkinter识别键入的希腊字符?

(我还尝试通过添加
TextInstance.bind('[h',lambda *忽略:TextInstance.insert(tkinter.INSERT,'ῆ'))重新绑定键盘快捷键
每个字符及其快捷方式;可以在英语键盘中使用(尽管也插入了激活事件的字符),但是在希腊多音阶键盘中,完全没有激活字母键上的绑定.)

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.)

推荐答案

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

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

unicode与tkinter和python完美配合

unicode works perfectly fine with tkinter and python

您可能还会对Unicode 希腊语 +

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.

我记得在希腊键盘上,您按住alt并按下要转换的字符,如果多次按下,它将再次更改.

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()

这篇关于在Tkinter中键入希腊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆