Tkinter Label with arabic text Tkinter Label with arabic text tkinter tkinter

Tkinter Label with arabic text


Since Arabic script is right to left, you can use the libraries: https://github.com/mpcabd/python-arabic-reshaper and https://github.com/MeirKriheli/python-bidi

Something like this can work:

# !/usr/bin/python3from tkinter import *import arabic_reshaperfrom tkinter import messageboxfrom bidi.algorithm import get_displaytext_to_be_reshaped = "ﺏﺎﻃﻭ"reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped)bidi_text = get_display(reshaped_text)top = Tk()top.geometry("70x30")B = Button(top, text = bidi_text)B.place(x=0,y=0)top.mainloop()

enter image description here


Hope this answer could help someone

This is how to add Arabic support to tkinter Label and Entry widgets

Edit:This fix for Arabic on linux only,but for Windows the bidi support will be handled by operating system and Arabic will be displayed correctly without a need for a fix.

you need to install AwesomeTkinter package pip install awesometkinter

example (source):

import tkinter as tkfrom awesometkinter.bidirender import add_bidi_support, render_textroot = tk.Tk()text = 'السلام عليكم'# text display incorrectly on linux without bidi supportdummyvar = tk.StringVar()dummyvar.set(text)tk.Label(root, textvariable=dummyvar, font='any 20').pack()# uncomment below to set a rendered text to first label# dummyvar.set(render_text(text))entry = tk.Entry(root, font='any 20', justify='right')entry.pack()lbl = tk.Label(root, font='any 20')lbl.pack()# adding bidi support for widgetsadd_bidi_support(lbl)add_bidi_support(entry)# now there is a new set() and get() methods to set and get text on a widgetentry.set(text)lbl.set('هذا كتاب adventure شيق')root.mainloop()

output:

example

Note: you can write Arabic directly in entry widget normally and text flow will be correct from right to left and letters will join correctly