【10】PyQt object-oriented

Table of contents

Qt window inheritance

Initialize ui


Qt window inheritance

Write a custom class that inherits QWidget

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class MyWindow(QWidget):

    def __init__(self, title):
        super().__init__()
        self.setWindowTitle(title)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MyWindow("窗口标题")
    window.show()

    sys.exit(app.exec_())

Implement the form through inheritanceQWidget

In the construction, the super function must be called, otherwise an error will occur

Initialize ui

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class MyWindow(QWidget):

    def __init__(self, title):
        super().__init__()
        self.setWindowTitle(title)
        self.init_ui()

    def init_ui(self):
        layout = QHBoxLayout()
    	# ---------------------------------

        # 在这里初始化界面内容
        
    	# ---------------------------------
        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MyWindow("窗口标题")
    window.show()

    sys.exit(app.exec_())

 

Guess you like

Origin blog.csdn.net/bug_love/article/details/134840953