1

I want to write a notepad in python with PyQt5 for persian language. how can I align the thext in QPlainTextEdit to right? this is my code:

from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit
from PyQt5.QtCore import Qt, QLocale

class TextBox(QPlainTextEdit):
    def persian(self):
        self.setFixedSize(640, 480)

        self.setLayoutDirection(Qt.RightToLeft)
        self.setLocale(QLocale(QLocale.Persian, QLocale.Iran))


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.GUI()

    def GUI(self):
        self.setWindowTitle("My title")
        self.setFixedSize(640, 480)

        self.text = TextBox(self)
        self.text.persian()


app = QApplication([])
window = MainWindow()
window.show()

app.exec_()
MSKF
  • 25
  • 8

1 Answers1

2

You can use a QTextEdit instead of QPlainTextEdit and use setAlignment(Qt.AlignRight), e.g.

from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtCore import Qt

class TextBox(QTextEdit):
    def persian(self):
        self.setFixedSize(640, 480)

        self.setLayoutDirection(Qt.RightToLeft)
        self.setLocale(QLocale(QLocale.Persian, QLocale.Iran))
        # set text alignment to AlignRight
        self.setAlignment(Qt.AlignRight)
Erich
  • 1,838
  • 16
  • 20
  • 1
    This does not answer the question. I personally require features only available in `QPlainTextEdit` (eg context menu & cursor control when read-only) – BlueChip Feb 23 '22 at 19:35