简体   繁体   English

Unicode 在 tkinter 中显示不正确

[英]Unicode Displays Incorrectly in tkinter

I am trying to display Indian languages in tkinter GUI.我正在尝试在 tkinter GUI 中显示印度语言。 I am using Python 3 and tkinter is of version 8.6.我使用的是 Python 3,tkinter 是 8.6 版。
In my code, python seems to handle the languages correctly because when I print them, the font and the character sequence are correct.在我的代码中,python 似乎正确处理语言,因为当我打印它们时,字体和字符序列是正确的。 But when I display the text on the tkinter GUI (Label, Text or Canvas) they are getting jumbled up or are not handled correctly.但是当我在 tkinter GUI(标签、文本或画布)上显示文本时,它们会变得混乱或没有正确处理。
The font seems to be not the issue as the language itself is correctly picked and many of the letters are correct.字体似乎不是问题,因为语言本身被正确选择并且许多字母都是正确的。 I had a look at this thread 8 years ago tkinter cannot display unicode characters correctly , my problem seems to be similar but there is no solution given to this either. 8 年前我看过这个线程tkinter 无法正确显示 unicode 字符,我的问题似乎很相似,但也没有给出解决方案。 I am pasting the simplified version of the code below.我正在粘贴下面代码的简化版本。 Please note - all fonts used are installed in my system.请注意 - 所有使用的字体都安装在我的系统中。

root = tk.Tk()
text = 'श्वसन प्रणाली में नाक गुहा, ट्रेकिआ और फेफड़े होते हैं'
labelcheck = ttk.Label(text=text, font = "Lohit\ Devnagri")
textcheck = tk.Text()
textcheck.insert(tk.END, text)
canvascheck = tk.Canvas(root,width=800, height=200)
canvascheck.create_text(200, 20, font="Lohit\ Devnagri", text=text)
labelcheck.grid(row = 0, column = 0)
textcheck.grid(row =0, column = 1)
canvascheck.grid(row = 1, column =0)
print(text)
root.mainloop()

The text printed in the console is an exact match of the text in the code.控制台中打印的文本与代码中的文本完全匹配。 The text in the tkinter UI is in the image link below. tkinter UI 中的文本位于下面的图片链接中。

Tkinter 屏幕

Note that there are minor differences for a person who does not know the language but for the native speaker/reader the changes are not trivial.请注意,对于不懂语言的人来说存在细微差别,但对于母语人士/读者来说,这些变化并非微不足道。

So, the question is does tkinter not handle all unicode characters correctly?那么,问题是 tkinter 是否不能正确处理所有 unicode 字符? Should I stop digging in this direction of making it work?我应该停止朝这个方向努力让它发挥作用吗? I am developing an application on Raspberrypi so I do not want to move away from tkinter as it is clearly very responsive and light weight.我正在 Raspberrypi 上开发一个应用程序,所以我不想离开 tkinter,因为它显然非常敏感且重量轻。

Any help here would be invaluable to me.这里的任何帮助对我来说都是无价的。

Edit 1: As per progmaticos suggestion, I took the unicode sequence of first few words and applied the normalize API to them.编辑 1:根据 progmaticos 建议,我采用了前几个单词的 unicode 序列并将规范化 API 应用于它们。 I still see the same issue - What python prints out is correct, while what is shown in tkinter's GUI is incorrect.我仍然看到同样的问题 - python 打印的内容是正确的,而 tkinter 的 GUI 中显示的内容不正确。

unicodetext = '\u0936\u094D\u0935\u0938\u0928\20\u092A\u094D\u0930\u0923\u093E\u0932\u0940'

text1 = unicodedata.normalize('NFC', unicodetext)
text2 = unicodedata.normalize('NFD', unicodetext)
text3 = unicodedata.normalize('NFKD', unicodetext)

The issue is not with tkinter.问题不在于 tkinter。 Looks like it is an OS issue.看起来这是一个操作系统问题。 Same application with python version same (3.8) on windows displays the unicode characters correctly.与 Windows 上的 python 版本相同 (3.8) 的相同应用程序正确显示 unicode 字符。 In ubuntu and rasbian the problem still persists.在 ubuntu 和 rasbian 中,问题仍然存在。 Will check on that issue in the coming days.将在未来几天检查该问题。 But the issue is nether tkinter's nor of python.但问题是 nether tkinter 和 python 的问题。 Thanks all for helping out.谢谢大家帮忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM