Ramprasad S
Ramprasad S

Reputation: 41

Unicode Displays Incorrectly in tkinter

I am trying to display Indian languages in tkinter GUI. I am using Python 3 and tkinter is of version 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. But when I display the text on the tkinter GUI (Label, Text or Canvas) they are getting jumbled up or are not handled correctly.
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. 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 Screen

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

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. I still see the same issue - What python prints out is correct, while what is shown in tkinter's GUI is incorrect.

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)

Upvotes: 3

Views: 1138

Answers (1)

Ramprasad S
Ramprasad S

Reputation: 41

The issue is not with tkinter. Looks like it is an OS issue. Same application with python version same (3.8) on windows displays the unicode characters correctly. In ubuntu and rasbian the problem still persists. Will check on that issue in the coming days. But the issue is nether tkinter's nor of python. Thanks all for helping out.

Upvotes: 1

Related Questions