3

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)
Ramprasad S
  • 41
  • 1
  • 4
  • 1
    I do not think I can help, and my eyes are not unicode enough to read your strings, but after reading the thread you mention, try new_string = `unicodedata.normalize('x', your_string)`, where x is either 'NFD' or 'NFC' to see if one of those gets displayed correctly. This encodes with several unicode points, or just one, for certain chars. And see if it changes anything. – progmatico Dec 28 '19 at 17:23
  • 1
    @snakecharmerb ,Yes the font renders correctly in other application. I have tried with multiple fonts. – Ramprasad S Dec 29 '19 at 02:07
  • @progmatico While I will try your solution and report what happened in some time the issue seems to be with "half" letters which are common in indian languages turned to full by the renderer and the sequence interchanged for certain combination of characters. – Ramprasad S Dec 29 '19 at 02:09
  • There were some Unicode-related enhancements in Tkinter in latest Python releases (3.7.6 and 3.8.1). Make sure you try your code in these versions first. – Aivar Jan 07 '20 at 08:11

1 Answers1

1

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.

Ramprasad S
  • 41
  • 1
  • 4