如何将 QPlainTextEdit 右对齐?

How to align QPlainTextEdit to right?

我想在 python 中使用 PyQt5 为波斯语写一个记事本。 如何将 QPlainTextEdit 中的文本右对齐? 这是我的代码:

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_()

您可以使用 QTextEdit 代替 QPlainTextEdit 并使用 setAlignment(Qt.AlignRight),例如

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)