PyQt5在主窗口中运行子小部件

时间:2020-05-12 14:52:59

标签: python pyqt5

我遇到以下问题:我有一个带有三个按钮的主窗口,还有一个小部件作为我的不同界面的占位符。 这是我的代码:

from PyQt5 import QtWidgets, QtCore
from ui.mainwindow import Ui_DeviceLauncher
from ui.testrun import Ui_Testrun

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.ui = Ui_DeviceLauncher()
        self.ui.setupUi(self)

        self.ui.changeWindowL.removeWidget(self.ui.changeWindow)
        self.ui.changeWindow.close()
        self.ui.changeWindow = Ui_Testrun() # This does not work how I want?
        self.ui.changeWindowL.addWidget(self.ui.changeWindow)
        self.ui.changeWindowL.update()

        self.setWindowTitle("Device Launcher")

#%%=======================================================================================
# Main
#=========================================================================================

app = QtWidgets.QApplication(sys.argv)

window = MainWindow()
window.show()

sys.exit(app.exec())

因此,我使用了两个用qtpy构建的ui文件,请参见以下内容: File1-主窗口:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_DeviceLauncher(object):
    def setupUi(self, DeviceLauncher):
        DeviceLauncher.setObjectName("DeviceLauncher")
        DeviceLauncher.resize(800, 460)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(DeviceLauncher.sizePolicy().hasHeightForWidth())
        DeviceLauncher.setSizePolicy(sizePolicy)
        DeviceLauncher.setMinimumSize(QtCore.QSize(800, 460))
        DeviceLauncher.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        DeviceLauncher.setLayoutDirection(QtCore.Qt.LeftToRight)
        DeviceLauncher.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
        DeviceLauncher.setAnimated(True)
        DeviceLauncher.setDockOptions(QtWidgets.QMainWindow.AllowTabbedDocks|QtWidgets.QMainWindow.AnimatedDocks)
        self.centralWidget = QtWidgets.QWidget(DeviceLauncher)
        self.centralWidget.setObjectName("centralWidget")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralWidget)
        self.gridLayout_3.setContentsMargins(11, 11, 11, 11)
        self.gridLayout_3.setSpacing(6)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setSpacing(6)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton_back = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton_back.setEnabled(False)
        self.pushButton_back.setStyleSheet("QPushButton {\n"
"color: white;\n"
"background-color: #2e9afe;\n"
"\n"
"height: 30px;\n"
"border-width: 0px;\n"
"border-radius: 3px;\n"
"min-width: 6em;\n"
"qproperty-text: \"BACK\";\n"
"}\n"
"\n"
"QPushButton:hover\n"
"{\n"
"background-color: #4a34ff;\n"
"}\n"
"\n"
"QPushButton:disabled {\n"
"background-color: gray;\n"
"color:white;\n"
"}")
        self.pushButton_back.setFlat(False)
        self.pushButton_back.setObjectName("pushButton_back")
        self.horizontalLayout.addWidget(self.pushButton_back)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.pushButton_user = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton_user.setStyleSheet("QPushButton {\n"
"color: white;\n"
"background-color: #2e9afe;\n"
"\n"
"height: 30px;\n"
"border-width: 0px;\n"
"border-radius: 3px;\n"
"min-width: 6em;\n"
"qproperty-text: \"USER\";\n"
"}\n"
"\n"
"QPushButton:hover\n"
"{\n"
"background-color: #0040ff;\n"
"}")
        self.pushButton_user.setObjectName("pushButton_user")
        self.horizontalLayout.addWidget(self.pushButton_user)
        self.pushButton_connect = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton_connect.setStyleSheet("QPushButton {\n"
"color: white;\n"
"background-color: #2e9afe;\n"
"\n"
"height: 30px;\n"
"border-width: 0px;\n"
"border-radius: 3px;\n"
"min-width: 6em;\n"
"qproperty-text: \"CONNECT\";\n"
"}\n"
"\n"
"QPushButton:hover\n"
"{\n"
"background-color: #0040ff;\n"
"}\n"
"\n"
"QPushButton:disabled {\n"
"background-color: gray;\n"
"color:white;\n"
"}\n"
"\n"
"QPushButton[connected=true] {\n"
"background-color: green;\n"
"qproperty-text: \"DISCONNECT\";\n"
"}\n"
"QPushButton[failed=true] {\n"
"background-color: red;\n"
"qproperty-text: \"FAILED\";\n"
"}")
        self.pushButton_connect.setObjectName("pushButton_connect")
        self.horizontalLayout.addWidget(self.pushButton_connect)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.changeWindow = QtWidgets.QWidget(self.centralWidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.changeWindow.sizePolicy().hasHeightForWidth())
        self.changeWindow.setSizePolicy(sizePolicy)
        self.changeWindow.setObjectName("changeWindow")
        self.changeWindowL = QtWidgets.QVBoxLayout(self.changeWindow)
        self.changeWindowL.setContentsMargins(9, 25, 9, 9)
        self.changeWindowL.setSpacing(0)
        self.changeWindowL.setObjectName("changeWindowL")
        self.verticalLayout.addWidget(self.changeWindow)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setSpacing(6)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.pushButton_settings = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton_settings.setStyleSheet("QPushButton {\n"
"color: white;\n"
"background-color: #2e9afe;\n"
"\n"
"height: 30px;\n"
"border-width: 0px;\n"
"border-radius: 3px;\n"
"min-width: 6em;\n"
"qproperty-text: \"SETTINGS\";\n"
"}\n"
"\n"
"QPushButton:hover\n"
"{\n"
"background-color: #0040ff;\n"
"}\n"
"\n"
"QPushButton:disabled {\n"
"background-color: gray;\n"
"color:white;\n"
"}")
        self.pushButton_settings.setObjectName("pushButton_settings")
        self.horizontalLayout_3.addWidget(self.pushButton_settings)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem1)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.gridLayout_3.addLayout(self.verticalLayout, 0, 0, 1, 1)
        DeviceLauncher.setCentralWidget(self.centralWidget)
        self.actionClear_Options = QtWidgets.QAction(DeviceLauncher)
        self.actionClear_Options.setObjectName("actionClear_Options")
        self.actionClear_All = QtWidgets.QAction(DeviceLauncher)
        self.actionClear_All.setObjectName("actionClear_All")

        self.retranslateUi(DeviceLauncher)
        QtCore.QMetaObject.connectSlotsByName(DeviceLauncher)

    def retranslateUi(self, DeviceLauncher):
        _translate = QtCore.QCoreApplication.translate
        DeviceLauncher.setWindowTitle(_translate("DeviceLauncher", "Device Launcher"))
        self.pushButton_back.setText(_translate("DeviceLauncher", "BACK"))
        self.pushButton_user.setText(_translate("DeviceLauncher", "USER"))
        self.pushButton_connect.setText(_translate("DeviceLauncher", "CONNECT"))
        self.pushButton_settings.setText(_translate("DeviceLauncher", "SETTINGS"))
        self.actionClear_Options.setText(_translate("DeviceLauncher", "Clear Options"))
        self.actionClear_All.setText(_translate("DeviceLauncher", "Clear All"))

文件2-第一个界面:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Testrun(object):
    def setupUi(self, Testrun):
        Testrun.setObjectName("Testrun")
        Testrun.resize(780, 364)
        Testrun.setStyleSheet("background-color: transparent;")
        self.gridLayout = QtWidgets.QGridLayout(Testrun)
        self.gridLayout.setContentsMargins(20, -1, 20, -1)
        self.gridLayout.setObjectName("gridLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.textBrowser = QtWidgets.QTextBrowser(Testrun)
        self.textBrowser.setObjectName("textBrowser")
        self.horizontalLayout.addWidget(self.textBrowser)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.textBrowser_terminal = QtWidgets.QTextBrowser(Testrun)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.textBrowser_terminal.sizePolicy().hasHeightForWidth())
        self.textBrowser_terminal.setSizePolicy(sizePolicy)
        self.textBrowser_terminal.setObjectName("textBrowser_terminal")
        self.verticalLayout.addWidget(self.textBrowser_terminal)
        self.progressBar = QtWidgets.QProgressBar(Testrun)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth())
        self.progressBar.setSizePolicy(sizePolicy)
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.verticalLayout.addWidget(self.progressBar)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.pushButton_stop = QtWidgets.QPushButton(Testrun)
        self.pushButton_stop.setObjectName("pushButton_stop")
        self.horizontalLayout_2.addWidget(self.pushButton_stop)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem)
        self.pushButton_start = QtWidgets.QPushButton(Testrun)
        self.pushButton_start.setObjectName("pushButton_start")
        self.horizontalLayout_2.addWidget(self.pushButton_start)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)

        self.retranslateUi(Testrun)
        QtCore.QMetaObject.connectSlotsByName(Testrun)

    def retranslateUi(self, Testrun):
        _translate = QtCore.QCoreApplication.translate
        Testrun.setWindowTitle(_translate("Testrun", "Form"))
        self.pushButton_stop.setText(_translate("Testrun", "PushButton"))
        self.pushButton_start.setText(_translate("Testrun", "PushButton"))

现在,我的问题是:是否可以像这样使用父主窗口和子窗口小部件,但是如何呢?还是有另一种方法可以做到这一点。经过研究,我还没有找到类似的用例。

0 个答案:

没有答案