代码之家  ›  专栏  ›  技术社区  ›  Kevin_ALA

Pyqt5:从对话框操作更改主窗口活动选项卡

  •  0
  • Kevin_ALA  · 技术社区  · 5 年前

    我在两个不同的班级有两个窗口。主窗口和对话框窗口。主窗口包含一个带有多个选项卡的Qtabwidget。对话框有一个按钮。

    在主窗口中,单击一个按钮,对话框打开。用户执行一些操作并单击“保存”按钮。单击按钮时,我希望对话框关闭,主窗口从初始选项卡更改为第二个选项卡。

    我的问题是我无法让tabwidget更改对话框save button click signal上的选项卡。

    尝试:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(392, 138)
            self.gridLayout = QtWidgets.QGridLayout(Dialog)
            self.gridLayout.setObjectName("gridLayout")
            self.frame = QtWidgets.QFrame(Dialog)
            self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
            self.frame.setObjectName("frame")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.verticalLayout = QtWidgets.QVBoxLayout()
            self.verticalLayout.setObjectName("verticalLayout")
            self.label = QtWidgets.QLabel(self.frame)
            font = QtGui.QFont()
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.label.setFont(font)
            self.label.setObjectName("label")
            self.verticalLayout.addWidget(self.label)
            self.lineEdit_vehicleName = QtWidgets.QLineEdit(self.frame)
            self.lineEdit_vehicleName.setObjectName("lineEdit_vehicleName")
            self.verticalLayout.addWidget(self.lineEdit_vehicleName)
            spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
            self.verticalLayout.addItem(spacerItem)
            self.verticalLayout_2.addLayout(self.verticalLayout)
            self.horizontalLayout = QtWidgets.QHBoxLayout()
            self.horizontalLayout.setObjectName("horizontalLayout")
            spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
            self.horizontalLayout.addItem(spacerItem1)
            self.pushButton_save = QtWidgets.QPushButton(self.frame)
            self.pushButton_save.setObjectName("pushButton_save")
            self.horizontalLayout.addWidget(self.pushButton_save)
            self.pushButton_cancel = QtWidgets.QPushButton(self.frame)
            self.pushButton_cancel.setObjectName("pushButton_cancel")
            self.horizontalLayout.addWidget(self.pushButton_cancel)
            self.verticalLayout_2.addLayout(self.horizontalLayout)
            self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
            # Dialog = QtWidgets.QDialog()
            self.pushButton_save.clicked.connect(Dialog.close)
            self.pushButton_save.clicked.connect(Ui_MainWindow.changeTab)
    
        def retranslateUi(self, Dialog):
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.label.setText(_translate("Dialog", "Enter Name:"))
            self.pushButton_save.setText(_translate("Dialog", "Save"))
            self.pushButton_cancel.setText(_translate("Dialog", "Cancel"))
    
    class Ui_MainWindow(object):
        def changeTab(self):
            self.tabWidget.setCurrentIndex(1)
    
        def openDialog(self):
            self.window = QtWidgets.QDialog()
            self.ui = Ui_Dialog()
            self.ui.setupUi(self.window)
            self.window.show()
    
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(548, 399)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
            self.gridLayout.setObjectName("gridLayout")
            self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
            self.tabWidget.setObjectName("tabWidget")
            self.tab = QtWidgets.QWidget()
            self.tab.setObjectName("tab")
            self.pushButton = QtWidgets.QPushButton(self.tab)
            self.pushButton.setGeometry(QtCore.QRect(240, 160, 75, 23))
            self.pushButton.setObjectName("pushButton")
            self.tabWidget.addTab(self.tab, "")
            self.tab_2 = QtWidgets.QWidget()
            self.tab_2.setObjectName("tab_2")
            self.tabWidget.addTab(self.tab_2, "")
            self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 548, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.tabWidget.setCurrentIndex(0)
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
    
    
            self.pushButton.clicked.connect(self.openDialog)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "PushButton"))
            self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
            self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
    
    
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   eyllanesc RAHUL KUMAR    5 年前

    PyQt建议不要修改Qt设计器生成的代码,而是创建一个继承自相应小部件的类。另一方面,save和cancel按钮必须分别连接到accept和reject方法。要获取信息,您可以使用exec_u(),它将返回与您按下的按钮相关联的值,如果是保存按钮,则必须更改QTabWidget的currentIndex

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(392, 138)
            self.gridLayout = QtWidgets.QGridLayout(Dialog)
            self.gridLayout.setObjectName("gridLayout")
            self.frame = QtWidgets.QFrame(Dialog)
            self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
            self.frame.setObjectName("frame")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.verticalLayout = QtWidgets.QVBoxLayout()
            self.verticalLayout.setObjectName("verticalLayout")
            self.label = QtWidgets.QLabel(self.frame)
            font = QtGui.QFont()
            font.setPointSize(10)
            font.setBold(True)
            font.setWeight(75)
            self.label.setFont(font)
            self.label.setObjectName("label")
            self.verticalLayout.addWidget(self.label)
            self.lineEdit_vehicleName = QtWidgets.QLineEdit(self.frame)
            self.lineEdit_vehicleName.setObjectName("lineEdit_vehicleName")
            self.verticalLayout.addWidget(self.lineEdit_vehicleName)
            spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
            self.verticalLayout.addItem(spacerItem)
            self.verticalLayout_2.addLayout(self.verticalLayout)
            self.horizontalLayout = QtWidgets.QHBoxLayout()
            self.horizontalLayout.setObjectName("horizontalLayout")
            spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
            self.horizontalLayout.addItem(spacerItem1)
            self.pushButton_save = QtWidgets.QPushButton(self.frame)
            self.pushButton_save.setObjectName("pushButton_save")
            self.horizontalLayout.addWidget(self.pushButton_save)
            self.pushButton_cancel = QtWidgets.QPushButton(self.frame)
            self.pushButton_cancel.setObjectName("pushButton_cancel")
            self.horizontalLayout.addWidget(self.pushButton_cancel)
            self.verticalLayout_2.addLayout(self.horizontalLayout)
            self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.label.setText(_translate("Dialog", "Enter Name:"))
            self.pushButton_save.setText(_translate("Dialog", "Save"))
            self.pushButton_cancel.setText(_translate("Dialog", "Cancel"))
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(548, 399)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
            self.gridLayout.setObjectName("gridLayout")
            self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
            self.tabWidget.setObjectName("tabWidget")
            self.tab = QtWidgets.QWidget()
            self.tab.setObjectName("tab")
            self.pushButton = QtWidgets.QPushButton(self.tab)
            self.pushButton.setGeometry(QtCore.QRect(240, 160, 75, 23))
            self.pushButton.setObjectName("pushButton")
            self.tabWidget.addTab(self.tab, "")
            self.tab_2 = QtWidgets.QWidget()
            self.tab_2.setObjectName("tab_2")
            self.tabWidget.addTab(self.tab_2, "")
            self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 548, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.tabWidget.setCurrentIndex(0)
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "PushButton"))
            self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
            self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
    
    
    class MainWindow(Ui_MainWindow, QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.setupUi(self)
            self.pushButton.clicked.connect(self.openDialog)
    
        @QtCore.pyqtSlot()
        def openDialog(self):
            self.dialog = Dialog()
            if  self.dialog.exec_() == QtWidgets.QDialog.Accepted:
                self.tabWidget.setCurrentIndex(1)
    
    
    class Dialog(Ui_Dialog, QtWidgets.QDialog):
        def __init__(self, parent=None):
            super(Dialog, self).__init__(parent)
            self.setupUi(self)
            self.pushButton_save.clicked.connect(self.accept)
            self.pushButton_cancel.clicked.connect(self.reject)
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())
    

    有关详细信息,请阅读 https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html