3

Does PySimpleGUI python library support RTL alignment?

I want to align Arabic text from right-to-left:

enter image description here

Code:

sg.Text("احب الطعام", font='12', size=(15, 1), background_color='#81a5b3')
Minions
  • 5,104
  • 5
  • 50
  • 91

2 Answers2

0

**just add to your sg.text() element.

justification="right"**

Mr-J03
  • 11
0

This problem is with Visual Studio itself rather than PySimpleGUI. You can make typing in Right-To-Left languages (like Arabic, Persian, Hebrew, etc) work by doing the following:

Solution

  1. In Visual Studio Code, press Ctrl+Shift+P
  2. Type >settings in search bar and open Open User Settings (JSON) *
  3. Add the line "editor.renderControlCharacters": false and save the settings.json file.

*You can set settings either to apply for everything the current Windows User opens, Default for the application so for all users' everything, or only per specific Workspace.

Now, after a recent VS update only this should be enough for it to work: it will recognize when you are typing in a RTL language. If for some reason it does not work, also do the following:

Potential/additional workaround

  • Manually add the unicode RTF character U+202B (or copy-paste from somewhere like here) at the start of the entry you want to type.

I've tested it just now and it does work fine, but doesn't seem to love copy/pasted text instead of typed which could be annoying for long entries if it decides not to work with you for some reason.

Keyboard toggle

You can bind a key to toggle between RTL and LTR to make life easier in case you go back and forth a lot.

  1. Ctrl+Shift+P

  2. Type in search bar and open Open Keyboard shortcuts (or press Ctrl+K)

  3. Add editor.action.toggleRenderControlCharacter to the search bar and set whatever keybind you want.

What this does is effectively add / remove the .json edit that is the solution (i.e. enable/disable RTL).

m-btt
  • 1
  • 2
  • This enables PySimpleGUI input to be right to left? What if not using Visual Studio? – Mike from PSG Jul 26 '23 at 15:54
  • It enables right to left on Visual Studio Code itself, it was a known issue only solved in recent years (https://github.com/microsoft/vscode/pull/136347). Even in Visual Studio 2022 the problem does not get replicated, I had to install VSCode to get it. You can test it by adding any RTL language (eg Arabic) to your OS keyboard and use the on-screen keyboard to type a few characters in VSC vs other IDEs (or even a web browser). The issue was with Pylance (comes bundled with Python in VSC). For other IDEs, they should work without any fix OR with the unicode character U+202B entered/copy pasted. – m-btt Jul 26 '23 at 18:19
  • Sorry, and to clarify: for user input via e.g. a Python app with PySimpleGUI user text input, no fix is needed. That's taken care of natively by the OS. Here's a pic of me testing it in Visual Studio 2022 on a demo GUI: https://cdn.discordapp.com/attachments/313716430321352704/1133827743755292722/image.png – m-btt Jul 26 '23 at 18:26
  • :D missed edit timeframe. Follows: for user input via eg a Python app with PySimpleGUI user text input, no fix *should* (dunno if would) be needed because that's taken care of natively by the OS when using a RTL keyboard language. Test on demo GUI (VSS2022 irrelevant, no changes done at all other than switch kb language): https://cdn.discordapp.com/attachments/313716430321352704/1133827743755292722/image.png I entered the middle row letter characters one by one starting from Enter to test it, and it seems to my untrained eye that it worked fine. Copy-paste behavior can still be weird though. – m-btt Jul 26 '23 at 18:37
  • Ah... OK, I'll remove my answer... it doesn't help anyone and only confuses. Thanks for the clarification. – Mike from PSG Jul 27 '23 at 17:11