PYQT5在botton上打开secondwindow在主窗口中单击,窗口使用对象

时间:2018-10-22 15:38:25

标签: python pyqt pyqt5

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(662, 512)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setGeometry(QtCore.QRect(130, 160, 113, 20))

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 662, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        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", "Push botton"))
        self.label.setText(_translate("MainWindow", "label1:"))

        self.pushButton.clicked.connect(self.btn_clk)

        MainWindow.show()

    def btn_clk(self):

        self.SW = Ui_SecondWindow()
        SecondWindow = QtWidgets.QMainWindow()
        self.SW.setupUi(SecondWindow)
        self.SW.show()

class Ui_SecondWindow(object):
    def setupUi(self,SecondWindow):
        SecondWindow.setObjectName("SecondWindow")
        SecondWindow.resize(662, 512)
        self.centralwidget = QtWidgets.QWidget(SecondWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")


        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.horizontalLayout.addLayout(self.verticalLayout)
        SecondWindow.setCentralWidget(self.centralwidget)
        self.menubar...

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

    def retranslateUi(self,SecondWindow):
        _translate = QtCore.QCoreApplication.translate
        SecondWindow.setWindowTitle(_translate("SecondWindow", "SecondWindow"))
        self.pushButton.setText(_translate("SecondWindow", "Push botton2"))

        self.pushButton.clicked.connect(self.btn_clk)

        SecondWindow.show()

    def btn_clk(self):
        return 0


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_())

我想在主窗口中单击“推动”按钮时打开第二个窗口,但是我尝试了一下,当我单击它时,主窗口关闭了。有人可以帮忙吗?

对于主窗口中的def_btn部分,任何人都可以检查问题吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(662, 512)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.label = QtWidgets.QLabel(self.centralwidget)          # +++
        self.label.setObjectName("label")                          # +++
        self.label.setGeometry(QtCore.QRect(130, 140, 113, 20))    # +++

        self.itemText = ""                                            # +++
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)       # +++
        self.lineEdit.setObjectName("lineEdit")                       # +++
        self.lineEdit.setGeometry(QtCore.QRect(250, 140, 100, 20))    # +++        

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setGeometry(QtCore.QRect(130, 160, 113, 20))

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 662, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        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", "Push botton"))
        self.label.setText(_translate("MainWindow", "label1:"))            ###

        self.pushButton.clicked.connect(self.btn_clk)

#        MainWindow.show()

    def btn_clk(self):

        SecondWindow = QtWidgets.QMainWindow(MainWindow)  # +
        self.SW = Ui_SecondWindow()
        self.SW.setupUi(SecondWindow)
# AttributeError: 'Ui_SecondWindow' object has no attribute 'show'        
#        self.SW.show()
        SecondWindow.show()                               # +

        self.SW.pushButton.clicked.connect(self.btn_clkSecondWindow)
        self.SW.tableWidget.itemPressed[QtWidgets.QTableWidgetItem].connect(self.cell_Activated)
# +        
    def btn_clkSecondWindow(self):
        self.lineEdit.setText(self.itemText)
# +        
    def cell_Activated(self, item):
        self.itemText = item.text()

class Ui_SecondWindow(object):
    def setupUi(self,SecondWindow):
        SecondWindow.setObjectName("SecondWindow")
        SecondWindow.resize(662, 512)
        self.centralwidget = QtWidgets.QWidget(SecondWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
# +++
        self.tableWidget = QtWidgets.QTableWidget()
        self.tableWidget.setRowCount(3)
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setHorizontalHeaderLabels(['Product', 'Description', 'Likes/UnLikes'])

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")

        self.verticalLayout.addWidget(self.tableWidget)
        self.verticalLayout.addWidget(self.pushButton)
        self.horizontalLayout.addLayout(self.verticalLayout)
        SecondWindow.setCentralWidget(self.centralwidget)
# ?       self.menubar...
        self.retranslateUi(SecondWindow)
        QtCore.QMetaObject.connectSlotsByName(SecondWindow)

    def retranslateUi(self,SecondWindow):
        _translate = QtCore.QCoreApplication.translate
        SecondWindow.setWindowTitle(_translate("SecondWindow", "SecondWindow"))
        self.pushButton.setText(_translate("SecondWindow", "Push botton2"))

#        self.pushButton.clicked.connect(self.btn_clk)
#        SecondWindow.show()
#    def btn_clk(self):
#        return 0

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_())

enter image description here