5

I'm using Tkinter for my Gui (Python). Until now the program has supported only LTR language. Now I need to also support RTL language (Hebrew).

I saw answers that handle with mirror text, but that's not my problem. I'm having an issue while using signs/punctuation (dot, comma etc.), and also while I'm writing both English and Hebrew in the same sentence. for example, I'm getting:

"שלום. welcome. מה שלומכם?"

When it should be:

"?מה שלומכם .welcome .שלום"

I'm lost. any suggestions?

RandomHash
  • 669
  • 6
  • 20
rinat dior
  • 49
  • 4
  • I am not sure but I don't think there is anything integrated in tkinter to do this., so you may need to implement something to do that yourself and write the rtl as ltr. – Xantium May 17 '18 at 08:59
  • Tags may help you - usually used for syntax highlighting, but if you can tag your Hebrew text, then run a function that reverses the direction of the text on the members of that tag. – RandomHash May 17 '18 at 09:11
  • 2
    You say by the end: *I'm getting:*: can you provide the original text to test? – Billal Begueradj May 17 '18 at 09:38
  • When it should be: "?מה שלומכם .welcome .שלום" – rinat dior May 21 '18 at 07:31
  • `tkinter` really isn't the best thing for this. However, Gtk's `TextView` does this automatically. You can type in a RTL language, then switch to English, then back to the RTL language, and it formats perfectly. If you want to write apps that handle languages like this well, I recommend you use Gtk instead of `tkinter`. There are, of course, other options out there, but Gtk is the one I have the most experience with, so that's the one I'm able to help you with if you have any questions :-). And no, I'm not affiliated with Gtk. – Sylvester Kruin Jan 05 '22 at 21:25

1 Answers1

0

Four years later, but as an exercise I wrote a function that changes the text from left-to-right to right-to-left.

text = "שלום. welcome. מה שלומכם?"

new_text = []
# list to hold each sentence

end_character_number = len(text) - 1
end_char = end_character_number

if text[end_char] == "?" or text[end_char] == "!" or text[end_char] == ".":
    new_text.append(text[end_char]) # adds final punctuation first
    text = text[:end_char] # removes final punctuation from left-to-right text

right_to_left_checker = end_char - 1 
rtl_checker = right_to_left_checker 

while rtl_checker >= 0:
    if text[rtl_checker] == "?" or text[rtl_checker] == "!" or text[rtl_checker] == ".":
        sentence = text[rtl_checker+1:].strip()
        new_text.append(sentence) # adds each sentence in right-to-left order
        new_text.append(" " + text[rtl_checker]) # add punctuation and space after
        text = text[:rtl_checker] # removes sentence from left-to-right text
    if rtl_checker == 0:
        new_text.append(text) # adds final sentence in right-to-left order
        text = text[:rtl_checker] # removes final sentence from left-to-right text
    rtl_checker = rtl_checker - 1 # moves punctuation checker one place right-to-left

right_to_left_sentence = ""
rtl_sent = right_to_left_sentence

for part in new_text:
    rtl_sent = rtl_sent + part # creates new right-to-left sentence

print(rtl_sent)
mishaeel
  • 18
  • 4