Unicode Displays Incorrectly in tkinter - python

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

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.

Related

How can I put giant writing in Python?

How could I make a word in giant text (for example “welcome” but giant)? I know that it's possible to change the console to make the font larger but I need it for just the word.
There are 2 ways to solve this problem
1 - Change the font in the python shell. Go to configure IDLE , go to the fonts tab and change the size value. Then apply the changes
2 - Using ASCII art. You can use ASCII art generators or use the python package pyfiglet(python version of figlet).
Example with pyfiglet
import pyfiglet
result = pyfiglet.figlet_format("Hello World")
print(result)
Pyfiglet also allows you to use many fonts , you can read their documentation for the everything else.
Hope this helps :)

Python pyautogui typewrite prints ~ in place of # on web page

I am facing a problem with printing the # symbol in a textbox on a webpage with Python pyautogui library. I am not able to print # (it prints ~ in a text box on a web page):
Input Example
my_string = "Here is an example for # symbol"
pyautogui typewrite (my_string)
Output
Here is an example for ~ symbol
Troubleshooting steps tried and additional information:
String is printed correctly with a # symbol in the command prompt if I use print (my_string)
I tried using ASCII character code Chr(35), but it still prints ~ and not #. Both chr(35) and Chr(126) produce ~ character only.
Tried changing keyboard language in Windows OS from UK English to US English (My laptop is the UK built)
I can type manually the # symbol in a text box on the web page. Text box accepts # as input. I can copy # symbol and paste # symbol manually in the text box.
Both # and ~ are on the same key in my laptop. Pressing Shift + # would generate ~ symbol. I tried the hotkey function and that didn't work.
I tried repr(a_string) function, but no help.
Tried Edge and Chrome browsers, same result.
I have Python 3.10.1 on Windows 10 OS.
I would appreciate it if someone can help in identifying the issue and suggest a solution.
Pyautogui tries to figure out which keys have to be shifted by using a hard coded list. This does not (yet) take the keyboard set-up into account. To correct it you can change this line (line 530) in the init.py file of pyautogui:
return character.isupper() or character in set('~!##$%^&*()_+{}|:"<>?')
Just remove the # character from it and it should work.

Chinese text in Tkinter, Python

is there anything I should do if I want to display Chinese characters in Tkinter in Python?
I want to diplay Chinese text on my labels. It seems like it works, kind of. It doesn't make the characters even - some of them are bold, some of them are not.
label = ttk.Label(text = "晚上好")
label.pack()
In this the "晚" appears bold, whereas "上好" does not. Is there anything I can do about this?

Psychopy:Is it possible to display 2 words in different colors within the same frame?

I want to display a sentence with words in different colors within the same frame . But all the code I've seen just change the color of the stimuli as a whole , not a part of it ...
Here's my code for a try ,but it failed
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from psychopy import visual,core
win = visual.Window([400,400])
sent=[u'先生',u'を呼んだ',u'学生が',u'教室に',u'入った。']
sent[0].color=[1,1,1]
sent[1].color=[1.0,-1,-1]
sentence=visual.TextStim(win,text=sent[0]+sent[1])
sentence.setAutoDraw(True)
win.flip()
I am wondering whether there's a way for me to change the text color before it becomes a visual.TextStim ?
No, the TextStim applies formatting (colour, italic, etc) to its entire contents. If you want words with different colours, they unfortunately each need to be in their own TextStim
An alternative is to the use TextBox class which I think allows per-character formatting, but only for monospace fonts:
http://www.psychopy.org/api/visual/textbox.html#psychopy.visual.TextBox
Having said that, I've found it doesn't work reliably at present, at least on Mac OS.

the Georgian language in tkinter. Python

I can not write on a standard Georgian language in the Text widget. instead of letters writes question marks .
when no tkinter, ie, when writing code, Georgian font recognized without problems. Plus, if I copy the word written in Georgian and inserted in the text widget, it is displayed correctly.
this is elementary code that displays the text box on the screen, where I want to write a word in Georgian.
import tkinter
root = tkinter.Tk()
txt = tkinter.Text(root)
txt.pack()
root.mainloop()
the first image shows how the word is displayed when the selected Georgian language.
the second shot, when I write in the code in Georgian, and define in advance the value of text field. in this case, the text in the field is displayed normally.
Okay, so here is how I achieved it:
First, make sure you have a Georgian font installed in your computer; if there is no any, then go download one (I downloaded mine from here);
Now, go to your tkinter program, and add your font to your Text widget:
txt = tkinter.Text(root, font=("AcadNusx", 16))
NOTE 1: My font name that supports Georgian is AcadNusx, but yours can be different;
NOTE 2: If you have not imported Font, then import it at the beginning of your program;
NOTE 3: Do not change your computer's font to Georgian, because you have already changed it inside the program, so make sure it is set to English.
The best answer I can determine so far is that there is something about Georgian and keyboard entry that tk does not like, at least not on Windows.
Character 'translation' is usually called 'transliteration'.
Tk text uses the Basic Multilingual Plane (the BMP, the first 2**16 codepoints) of Unicode. This includes the Georgian alphabet. The second image shows that the default Text widget font on your system is quite capable of displaying Georgian characters once the characters are in the widget. So a new display font does not seem to be the solution to your problem.
('ქართული ენა' is visible on Firefox because FF is unicode based and can display most if not all of the BMP.)
It looks like the problem is getting the proper codes to tk without going through your editor. What OS and editor are your using. How did you enter the mixed-alphabet line similar to
txt.insert('1.0', 'ქართული ენა') # ? (I cannot copy the image string.)
How are you running the Python code? If you cut the question marks from the first image and insert into
for c in '<insert here>': print(ord(c))
what do you see?
You need a 'Georgian keyboard entry' or 'input method' program (Google shows several) for your OS that will let you switch back and forth between sending ascii and Geargian codes to any program reading the keyboard.
Windows now comes with this, with languages activated on a case-by-case basis. I already had Spanish entry, and with it I can enter á and ñ both here and into IDLE and a fresh Text box. However, when I add Georgian, I can type (randomly ;-) ჰჯჰფგეუგსკფ here (in FireFox, also MS Edge) but only get ?????? in tk Text boxes. And these are actual ascii question marks, ord('?') = 63, rather that replacements for codes that cannot be represented. Japanese also works with tk-Text based IDLE. So the problem with Georgian is not generic to all non-latin alphabets.

Categories

Resources