首页 > 其他分享 >PyQt 主窗口、子窗口交互

PyQt 主窗口、子窗口交互

时间:2023-02-08 10:56:55浏览次数:47  
标签:__ SubWindow 窗口 pushButton centralwidget self PyQt 交互 MainWindow

主窗口main.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, QLabel, QMainWindow, QPushButton,
    QSizePolicy, QTextEdit, QWidget)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(409, 231)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(40, 20, 80, 25))
        self.label = QLabel(self.centralwidget)
        self.label.setObjectName(u"label")
        self.label.setGeometry(QRect(40, 80, 71, 17))
        self.textEdit = QTextEdit(self.centralwidget)
        self.textEdit.setObjectName(u"textEdit")
        self.textEdit.setGeometry(QRect(40, 100, 331, 70))
        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", None))
        self.label.setText(QCoreApplication.translate("MainWindow", u"Text Show", None))
    # retranslateUi

子窗口 sub.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, QLabel, QMainWindow, QPushButton,
    QSizePolicy, QTextEdit, QWidget)

class Ui_SubWindow(object):
    def setupUi(self, SubWindow):
        if not SubWindow.objectName():
            SubWindow.setObjectName(u"SubWindow")
        SubWindow.resize(409, 231)
        self.centralwidget = QWidget(SubWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(40, 20, 80, 25))
        self.label = QLabel(self.centralwidget)
        self.label.setObjectName(u"label")
        self.label.setGeometry(QRect(40, 80, 71, 17))
        self.textEdit = QTextEdit(self.centralwidget)
        self.textEdit.setObjectName(u"textEdit")
        self.textEdit.setGeometry(QRect(40, 100, 331, 70))
        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"Submit", None))
        self.label.setText(QCoreApplication.translate("SubWindow", u"Text Show", None))
    # retranslateUi

主程序

import sys
from PySide6 import QtCore,QtWidgets
from PySide6.QtWidgets import *

from main import Ui_MainWindow
from sub import Ui_SubWindow


class main(QMainWindow):
    def __init__(self):
        super().__init__()    
        self.ui = Ui_MainWindow()      
        self.ui.setupUi(self) 
        self.__create_connections()
        

    def __create_connections(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.submit)
        

    def submit(self):
        m.ui.textEdit.append(self.ui.textEdit.toPlainText())

    def _open(self):
        self.show()
        

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

标签:__,SubWindow,窗口,pushButton,centralwidget,self,PyQt,交互,MainWindow
From: https://www.cnblogs.com/conpi/p/17100949.html

相关文章