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.
Related
I'm using Python 3.10 to implement a classical mechanics problem, and I want to print a message to the console asking the user to input the initial velocities. I am using x, y, and z as coordinates so ideally I want to denote the velocity components as vx, vy, and vz.
Originally I thought of using unicode subscirpts, but apparently they don't exist for y and z (as this other SO answer explains).
Of course I could just display (v_x, v_y, v_z), but I wanted it to look a bit more polished. Is there an easy way to display non-unicode subscripts in Python? Or otherwise, some very bare-bones UI package where I can have more freedom in formatting the text (like using LaTeX, or Markdown)?
No -
The terminal emulator (or command line) can only display characters, and does not allow for unlimited character transformations, as it is possible with text on a web browser or in a graphic interface.
ALthough there are special character sequences that can trigger special features such as foreground and background color, underline and blinking, those have to be implemented by the terminal emulator program itself, and appart from a subset, there is no universal code convention. The closest one, are what we usually call "ANSI escape code sequences" do not provide for a subscript or super-script convert - you can check the available codes on wikipedia - those will work in most terminal programs for Linux and MacOS and most custom terminal programas in windows (including the one called "terminal" in the microsoft store), but not on the default "cmd" app which cames pre-installed in windows.
(There is a Python package called "colorama" which tries to overcome this limitation to cmd, allowing cross-platform terminal programs able to display rich text - but it will filter out the codes for using fullcolor in terminal programs that accept them, so it is not always a good idea)
All that said, the tables as they are in the linked wikepdia article may be a bit confusing - but for shorten: "CSI" is the sequence "\x1b[" - where "\x1b"is the "ESC" character (decimal 27) , and "[" is a literal "open square bracket" char - the "SGR" sequence is"\x1b[<list-of-parameters-separated-by-;>m"` (again, "m" here is just the plain letter "m" closing a sequence of numeric codes that may change the way the terminal will display some text.
So, for front-face red text, you may want to do:
print("\x1b[31mThis text in red\x1b[39m normal color").
(note that the numbers are also plain decimal strings with the digits)
You will note that the code "74" is reserved for "subscript" code - however, I don't know of a terminal emulator which implements it.
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 :)
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?
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.
Is there any way that I can color a word in a string before I even addstr it to the curses screen? I have tried to do this at a low-level by concatenating things like "\033[31m" (and then "\033[0m" at the end) to the string, but curses does not recognize that and just spits out something like: this is a ^[[31mlink^[[0m. If you do not understand so far, this may help:
# pseudocode
a = "this is a link"
replace "link" in a with colorRed("link")
Thank you.
No: programs that use curses do not send escape sequences. Instead, they pass strings and attributes. The curses library puts those together and sends escape sequences.
In the C interface for curses, that could be done per-character by OR'ing the attribute onto chtype values (or packing cchar_t using setcchar). Alternatively, one could set an attribute on the window and write a string using that attribute with addstr.
Python provides (according to its documentation) a different interface to addstr. The section Attributes and Color shows this example:
stdscr.addstr(0, 0, "Current mode: Typing mode", curses.A_REVERSE)
to add a reverse-video string, and this:
stdscr.addstr("Pretty text", curses.color_pair(1))
to add a string using color pair #1. Color pairs combine foreground (text) and background colors.
Python's interface to changing the window attributes more closely resembles the C interface: it provides attrset, attron, bkgd methods as shown in its documentation for Window Objects.
As a general guideline, you would not get good results by writing your own escape sequences directly (bypassing the curses library):
curses uses its own output buffering, which may not be connected to your standard output buffer (so things may be written in an unexpected order)
if you wrote something that changed the screen's appearance, curses would not know about that, and the result would not be helpful.