简体   繁体   中英

Centring a QTextEdit in PyQt5

My Experience:

I am confident using python in general but I am new to PyQt5.

What I am working on:

I am currently using PyQt5 for Python 3.6 to build a text editor, it has all of the basic features a normal text editor would have eg saving files, opening files, changing fonts, etc.

My issue

Currently I have built a basic layout for my program containing a menu bar with various buttons, I am now at the point of adding the 'textEdit' box into the program but it does not appear in the centre of the UI:

文本框

The text box is also relatively small compared to the size of the overall window, so currently I am left with a small, top-left aligned text box. How can I centre this text box and make it span the whole window.

My main class:

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Text Editor'
        self.left = 10
        self.top = 10
        self.width = 1080
        self.height = 920
        self.text = QTextEdit(self)

    self.initUI()

You can add the QTextEdit as the central widget of QMainWindow as shown below:

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Text Editor'
        self.left = 10
        self.top = 10
        self.width = 1080
        self.height = 920

        self.text = QTextEdit(self)
        self.setCentralWidget(self.text)

Screenshot:

在此处输入图片说明

Or add it inside the central widget through a layout:

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Text Editor'
        self.left = 10
        self.top = 10
        self.width = 1080
        self.height = 920

        self.widget = QWidget(self)

        self.text = QTextEdit(self.widget)
        self.widget.setLayout(QVBoxLayout())
        self.widget.layout().addWidget(self.text)

        self.setCentralWidget(self.widget)

Screenshot:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM