【Python】Pyqt5 主窗口调用子窗口demo

背景:
通过主窗口的按钮调用其对应的窗口,代码比较简单,直接看,不懂的欢迎留言;
一共两个代码:
ui_first.py :实现的是主窗口
ui_second.py:实现的是子窗口

点击开始按钮调用子窗口,点击关闭退出窗口;

ui_first.py

import sys
import os
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QHBoxLayout
from .ui_sencond import UiMainTwo
from PyQt5.QtCore import Qt


class UiMain(QWidget):
    def __init__(self, parent=None):
        super(UiMain, self).__init__(parent)

        self.setWindowTitle('main_wsw')
        self.resize(400, 200)
        self.initUI()

    def initUI(self):
        layout = QHBoxLayout()
        self.start = QPushButton("开始")
        self.start.clicked.connect(self.action_b)
        layout.addWidget(self.start)

        self.end = QPushButton("结束")
        self.end.clicked.connect(self.close)
        layout.addWidget(self.end)
        layout.addStretch()
        self.setLayout(layout)

    def action_b(self):
        self.ui_2 = UiMainTwo()
        self.ui_2.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = UiMain()
    main.show()
    sys.exit(app.exec_())

ui_second.py

import sys
import os
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QMenu, \
    QSizePolicy, QVBoxLayout, QHBoxLayout, QLabel


class UiMainTwo(QWidget):
    def __init__(self, parent=None):
        super(UiMainTwo, self).__init__(parent)
        self.setWindowTitle('second_wsw')
        self.resize(300, 200)
        self.move(800, 400)
        self.initUI2()

    def initUI2(self):
        layout = QVBoxLayout()
        self.label1 = QLabel('label1: hi i am wsw', self)
        layout.addWidget(self.label1)

        self.label2 = QLabel('label2: hi all ...', self)
        layout.addWidget(self.label2)
        layout.addStretch()

        self.label3 = QLabel('label3: hi all ...', self)
        layout.addWidget(self.label3)

        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = UiMainTwo()
    sys.exit(app.exec_())

【Python】Pyqt5 主窗口调用子窗口demo_第1张图片

你可能感兴趣的:(Python,qt5,python,qt,ui)