首页 > 其他分享 >PyQt 打开子窗口

PyQt 打开子窗口

时间:2023-02-07 15:24:12浏览次数:44  
标签:SubWindow 窗口 pushButton self PyQt ui import 打开 MainWindow

实现功能

在MainWindow点击按钮“Open New Window”打开SubWindow
在SubWindow 点击按钮“Close the Window” 关闭 SubWindow

有QtDesigner设计两个窗口,转化为main_ui.py、sub_ui.py

main_ui.py

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QMainWindow, QPushButton, QSizePolicy,
    QWidget)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(331, 204)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(49, 50, 161, 25))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
        self.pushButton.setText(QCoreApplication.translate("MainWindow", u"Open New Windown", None))
    # retranslateUi

sub_ui.py

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QMainWindow, QPushButton, QSizePolicy,
    QWidget)

class Ui_SubWindow(object):
    def setupUi(self, SubWindow):
        if not SubWindow.objectName():
            SubWindow.setObjectName(u"SubWindow")
        SubWindow.resize(348, 182)
        self.centralwidget = QWidget(SubWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(90, 50, 151, 25))
        SubWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(SubWindow)

        QMetaObject.connectSlotsByName(SubWindow)
    # setupUi

    def retranslateUi(self, SubWindow):
        SubWindow.setWindowTitle(QCoreApplication.translate("SubWindow", u"SubWindow", None))
        self.pushButton.setText(QCoreApplication.translate("SubWindow", u"Close the Window", None))
    # retranslateUi

主程序

import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *
from main_ui import Ui_MainWindow
from sub_ui import Ui_SubWindow

class main(QMainWindow):
    def __init__(self):
        super().__init__()    
        self.ui = Ui_MainWindow()      
        self.ui.setupUi(self) 
        self.ui.pushButton.clicked.connect(self.open_subwindow)  

    def open_subwindow(self):
        s._open()

                  
class sub(QMainWindow):
    def __init__(self):
        super().__init__()    
        self.ui = Ui_SubWindow()      
        self.ui.setupUi(self) 
        self.ui.pushButton.clicked.connect(self.close)

    def _open(self):
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    m = main()
    m.show()
    s = sub()
    sys.exit(app.exec())

标签:SubWindow,窗口,pushButton,self,PyQt,ui,import,打开,MainWindow
From: https://www.cnblogs.com/conpi/p/17098535.html

相关文章