Use arabic text with tkinter Use arabic text with tkinter tkinter tkinter

Use arabic text with tkinter


Tkinter doesn't have a bidi support (According to tk's source code), which means RTL(Right-To-Left) languages like Arabic will be displayed incorrectly with 2 problems:

1- letters will be displayed in reverse order.

2- letters are not joined correctly.

the reason Arabic is being displayed correctly on windows is because bidi support is handled by operating system, and this is not the case in Linux

to fix this problem on linux you can use AwesomeTkinter package, it can add bidi support to labels and entry (also while editing)

import tkinter as tkimport awesometkinter as atkroot = tk.Tk()welcMsg = 'مرحبا'# text display incorrectly on linux without bidi supporttk.Label(root, text=welcMsg).pack()entry = tk.Entry(root, justify='right')entry.pack()lbl = tk.Label(root)lbl.pack()# adding bidi support for widgetsatk.add_bidi_support(lbl)atk.add_bidi_support(entry)# Now we have a new set() and get() methods to set and get text on a widget# these methods added by atk.add_bidi_support() and doesn't exist in standard widgets.entry.set(welcMsg)lbl.set(welcMsg)root.mainloop()

output:

enter image description here

note: you can install awesometkinter by pip install awesometkinter


You simply need to set your encoding to UTF-8 by making this line the first one (before your code) in your Python file: # -- coding: UTF-8 --

This is a code sample:

# -*- coding: UTF-8 -*-from Tkinter import *root = Tk()root.title('Alram')root.geometry("1500x600")mytext= 'ذكرت تقارير' #Arabic textmsg = Message(root, bg="red", text= mytext, justify='right')msg.config(font=('times', 72, 'bold'))exit_button = Button(root, width=10, text='Exit', command=root.destroy)exit_button.pack()msg.pack(fill=X)root.mainloop()

here is the original answer:Arabic text in TKinter