5

I'm using a RTL language and I need my text to be RTL. Is there a way to do it? And How can I justify my text? Example:

from tkinter import *
from tkinter.constants import *
root = Tk()
text = Text(root,,font=('Tahoma',8))#I need RTL and Right justified text!
text.grid()
scrl = Scrollbar(root, command=text.yview)
text.config(yscrollcommand=scrl.set)
scrl.grid(row=0, column=1, sticky='ns')
root.mainloop()
6
  • See: stackoverflow.com/questions/4150053/… and wiki.tcl.tk/699
    – furas
    Nov 30, 2013 at 23:16
  • 2
    @furas while that question tackles the same problem, the given answer uses a terrible hack and is from 2 years ago. Surely there's a better solution than that, no? (not OP, just an interested bystander)
    – yuvi
    Nov 30, 2013 at 23:18
  • @furas Can you tell me about how to use justify? And I need a better answer such as tkinter internal methods.
    – hamidfzm
    Nov 30, 2013 at 23:18
  • Looking for the answer, I have to say that the docs for TKinter are horrifyingly bad, include partial explanations (or outdated ones) and bad links going nowhere (i.e. personally, I would drop TKinter entirely and look for an alternative)
    – yuvi
    Nov 30, 2013 at 23:25
  • I only show that there is problem with RTL. Problem was 2 years ago and probably it is still today. Maybe there is better solutin, maybe solution is even somewhere on SO but I don't know it.
    – furas
    Dec 1, 2013 at 0:01

3 Answers 3

2

i modified your code and it's worked!..

from tkinter import *
from tkinter.constants import *
root = Tk()
text = Text(root,,font=('Tahoma',8))#I need RTL and Right justified text!

text.tag_configure('tag-right', justify='right')
text.insert('end', 'text ' * 10, 'tag-right')
text.grid()

scrl = Scrollbar(root, command=text.yview)
text.config(yscrollcommand=scrl.set)
scrl.grid(row=0, column=1, sticky='ns')
root.mainloop()

in fact i add 2 lines code that set justify=CENTER for a Text widget fails: there is no such option for the widget.

What you want is to create a tag with the parameter justify. Then you can insert some text using that tag (or you can insert any text and later apply the tag to a certain region)... Good luck! :)

2
  • it does not change RTL logical flow. RTL != justify; it's something completely different (that's sometimes mixed up) Jul 24, 2022 at 23:53
  • Typo error remove extra comma text = Text(root,, Nov 16, 2022 at 11:25
0

1 Answer will solve that letters inside the word wrote correctly from right to left, but still, the new line position is on the left, and sentences do not begin from right to left like this example.

کلمات در فارسی باید حتما از راست شروع شوند نه از میان صفحه ، و خط جدید هم باید از طرف راست شروع شود .

if you see the second line begins from the middle of the page, like this. consider next line begins in the middle.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 9, 2022 at 14:53
0

This is not an direct answer for the current question; but is somehow related. In case of someone needs to write RTL in an Entry (Alternative to Textbox or Input in Tkinter) for farsi (persian), arabic and/or other RTL languages; you can configure your Entry like this:

from tkinter import *

win = Tk()
ent = Entry(win, justify="right")
ent.pack()
win.mainloop()

enter image description here

  • Note that: Chars of text is broken inside Entry; I don't know what exactly is wrong about this in Ubuntu; but as far as i know there should not be a problem in windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.