Creating a Tkinter Application for Farsi/Persian Language Support with Correct RTL Display

134 Views Asked by At

I am working on a Tkinter application where I need to support the Farsi/Persian language. The title displays correctly, but the rest of the widgets don't show Persian text as expected. I want the text to be right-to-left (RTL), and the Persian letters should stick to each other.

Here's a simplified version of my code:

import tkinter as tk
from tkinter import messagebox

class InvoiceApp:
    def __init__(self, master):
        self.master = master
        self.master.title("نرم‌افزار صورتحساب")

        # لیست محصولات
        self.products = []

        # ایجاد ویجت‌ها
        self.product_label = tk.Label(master, text='نام محصول:', justify='right')
        self.product_entry = tk.Entry(master)
        self.price_label = tk.Label(master, text="قیمت:")
        self.price_entry = tk.Entry(master)
        self.add_button = tk.Button(master, text="افزودن به صورتحساب", command=self.add_to_invoice, justify='right')
        self.create_invoice_button = tk.Button(master, text="ساخت صورتحساب", command=self.create_invoice)
        self.send_email_button = tk.Button(master, text="ارسال ایمیل", command=self.send_email)

    def add_to_invoice(self):
        # افزودن محصول به لیست
        pass

    def create_invoice(self):
        # ساخت صورتحساب
        pass

    def send_email(self):
        # ارسال ایمیل
        pass

if __name__ == "__main__":
    root = tk.Tk()
    app = InvoiceApp(root)
    root.mainloop()

enter image description here I have tried setting the font and anchor properties, but it doesn't seem to work as expected. Can someone guide me on how to configure Tkinter widgets to display Persian text correctly, right-to-left, and with letters sticking together?

Any help would be greatly appreciated! Thank you.

1

There are 1 best solutions below

4
toyota Supra On

I want the text to be right-to-left (RTL), and the Persian letters should stick to each other.

The problem can be fixed.

You are missing layout manager such as place(), pack() or grid().

So, I used grid().

Add keyword justify='right' for both Entry widgets.

Btw, You haven't mention about Text widget. Actually, I leave it up to you.

Snippet:

class InvoiceApp:
    def __init__(self, master):
        self.master = master
        self.master.title("نرم‌افزار صورتحساب")

        # لیست محصولات
        self.products = []

        # ایجاد ویجت‌ها
        self.product_label = tk.Label(master, text='نام محصول:', justify='right')
        self.product_label.grid(row=0, column=0, sticky='w')               
        
        self.product_entry = tk.Entry(master, justify='right')
        self.product_entry.grid(row=0, column=1, padx=15, sticky='n')
        
        self.price_label = tk.Label(master, text="قیمت:", justify='right')
        self.price_label.grid(row=0, column=2,   sticky='n')
        
        self.price_entry = tk.Entry(master, justify='right')
        self.price_entry.grid(row=0, column=3, padx=15, sticky='n')
        
        self.add_button = tk.Button(master, text="افزودن به صورتحساب", command=self.add_to_invoice, justify='right')
        self.add_button.grid(row=1, column=1,  columnspan=3, pady=5, sticky='n')
        
        self.create_invoice_button = tk.Button(master, text="ساخت صورتحساب", command=self.create_invoice)
        self.create_invoice_button.grid(row=2, column=1, columnspan=3, pady=5,  sticky='n') 

        
        self.send_email_button = tk.Button(master, text="ارسال ایمیل", command=self.send_email)
        self.send_email_button.grid(row=3, column=1, columnspan=3, pady=5,  sticky='n')

        #st = ScrolledText(master, width=50,  height=10)
        #st.grid(row=4, column=1, columnspan=3, pady=5,  sticky='n')

        self.text = tk.Text(master, font=('Tahoma',8))
        self.text.tag_configure('tag-right', justify='right')
        self.text.insert('end', 'This is a Text widget demo', 'tag-right')
        self.text.grid(row=4, column=1, columnspan=3, pady=5,  sticky='n')

        scrl = tk.Scrollbar(master, command=self.text.yview)
        self.text.config(yscrollcommand=scrl.set)
        scrl.grid(row=4, column=4, sticky='ns')

Screenshot:

enter image description here