How to display non Latin character on output text console correctly?

Viewed 199

I'm new in Python. I'm experimenting to make "IDE" to run Python script that display non-Latin character, especially Indonesian local scripts (Javanese, Balinese, Buginese etc.) that have been included in the Unicode Standard. Please consider installing the Indonesian Unicode fonts to render the characters correctly.

Here is the code:

from tkinter import *

from tkinter.filedialog import asksaveasfilename, askopenfilename
import subprocess

compiler = Tk()
compiler.title ('ᮊᮧᮙ᮪ᮕᮤᮜᮨᮁ ᮞᮥᮔ᮪ᮓ')
file_path = ''

def set_file_path(path):
    global file_path
    file_path = path

def save_as():
    if file_path == '':
        path = asksaveasfilename(filetypes=[('Python Files', '*.py')])
    else:
        path = file_path
    with open(path, 'w') as file:
        code = editor.get('1.0', END)
        file.write(code)
        set_file_path(path)

def open_file():
    path = askopenfilename(filetypes=[('Python Files', '*.py')])
    with open(path, 'r') as file:
        code =file.read()
        editor.delete('1.0', END)
        editor.insert('1.0', code)
        set_file_path(path)

def run():
    if file_path == '':
        save_prompt = Toplevel()
        text = Label(save_prompt, text='ᮞᮤᮙ᮪ᮕᮨᮔ᮪ ᮠᮩᮜ ᮊᮧᮓᮩ ᮃᮔ᮪ᮏᮩᮔ᮪')
        text.pack()
        return
    command = f'python {file_path}'
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    output, error = process.communicate()
    code_output.insert('1.0', output)
    code_output.insert('1.0', error)


menu_bar = Menu(compiler)

file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label='ᮘᮥᮊ', command=open_file)
file_menu.add_command(label='ᮞᮤᮙ᮪ᮕᮨᮔ᮪', command=save_as)
file_menu.add_command(label='ᮞᮤᮙ᮪ᮕᮨᮔ᮪ ᮔᮥ ᮞᮦᮏᮦᮔ᮪', command=save_as)
file_menu.add_command(label='ᮊᮜᮥᮃᮁ', command=exit)
menu_bar.add_cascade(label='ᮘᮨᮁᮊᮞ᮪', menu=file_menu)

run_bar = Menu(menu_bar, tearoff=0)
run_bar.add_command(label='ᮏᮜᮔ᮪ᮊᮩᮔ᮪', command=run)
menu_bar.add_cascade(label='ᮏᮜᮔ᮪ᮊᮩᮔ᮪', menu=run_bar)

compiler.config(menu=menu_bar)

editor = Text()
editor.pack()

code_output = Text(height=10)
code_output.pack()

compiler.mainloop()

I want to print the non-Latin characters, for example:

print('Sundanese script')
print('ᮃᮊ᮪ᮞᮛ ᮞᮥᮔ᮪ᮓ')

unfortunately the result shows this:

Sundanese script
ᮃᮊ᮪ᮞᮛ ᮞᮥᮔ᮪ᮓ

or the code like this:

import unicodedata

u = chr(233) + chr(0x03B2) + chr(0xA991) + chr(0x1B11) + chr(0x1B91) + chr(0x1BE1)

for i, c in enumerate(u):
    print(i, '%04x' % ord(c), unicodedata.category(c), u[i], end=" ")
    print(unicodedata.name(c))

will display this:

0 00e9 Ll é LATIN SMALL LETTER E WITH ACUTE
1 03b2 Ll β GREEK SMALL LETTER BETA
2 a991 Lo ꦑ JAVANESE LETTER KA MURDA
3 1b11 Lo ᬑ BALINESE LETTER OKARA
4 1b91 Lo ᮑ SUNDANESE LETTER NYA
5 1be1 Lo ᯡ BATAK LETTER CA

But the import unicodedata script could be displayed well on the terminal:

>>> import unicodedata
>>> 
>>> u = chr(233) + chr(0x03B2) + chr(0xA991) + chr(0x1B11) + chr(0x1B91) + chr(0x1BE1)
>>> 
>>> for i, c in enumerate(u):
...     print(i, '%04x' % ord(c), unicodedata.category(c), u[i], end=" ")
...     print(unicodedata.name(c))
... 
0 00e9 Ll é LATIN SMALL LETTER E WITH ACUTE
1 03b2 Ll β GREEK SMALL LETTER BETA
2 a991 Lo ꦑ JAVANESE LETTER KA MURDA
3 1b11 Lo ᬑ BALINESE LETTER OKARA
4 1b91 Lo ᮑ SUNDANESE LETTER NYA
5 1be1 Lo ᯡ BATAK LETTER CA

Can anyone point out the problem?

0 Answers
Related