1

我的代码中有两个窗口首先我想打开包含 btn1 的类 first() 窗口。当我单击 btn1 时,我想打开一个新窗口并将其替换为我以前的窗口(即在当前窗口本身中打开新窗口),因此在单击 btn1 时,将显示一个包含 btn2 的类 second() 的新窗口。现在,当我按下 btn2 时,我想在终端上打印“hi”,但不知何故,连接插槽或某些东西无法正常工作。你能帮帮我吗?这是我的代码

class first(loginWindow.Ui_MainWindow):
    def __init__(self, MainWindow):
        super(first, self).setupUi(MainWindow)
        self.btn1.clicked.connect(self.loginFunc)

    def loginFunc(self):
        MainWindow.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        displayUi = second(MainWindow)
        MainWindow.show()

class second(displayWindow.Ui_MainWindow):
    def __init__(self, MainWindow2):
        super(second, self).setupUi(MainWindow2)
        self.btn2.clicked.connect(self.dispTable)

    def dispTable(self):
        print("hi")

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    loginUi = first(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

first() 和 second() 类继承自 qt-designer 工具创建的其他类。

4

1 回答 1

0

试试看:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *

class Ui_MainWindow1(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(264, 248)
        MainWindow.setMinimumSize(QtCore.QSize(264, 248))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.submitBtn = QtWidgets.QPushButton(self.centralwidget)
        self.submitBtn.setObjectName("submitBtn")
        self.gridLayout.addWidget(self.submitBtn, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "first MainWindow"))    
        self.submitBtn.setText(_translate("MainWindow", "Go second"))

class Ui_MainWindow2(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(264, 248)
        MainWindow.setMinimumSize(QtCore.QSize(264, 248))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "second MainWindow"))    
        self.pushButton.setText(_translate("MainWindow", "hi"))

class first(QMainWindow, Ui_MainWindow1):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.submitBtn.clicked.connect(self.loginFunc)

    def loginFunc(self):
        self.displayUi = second() 
        self.hide()
        self.displayUi.show()

class second(QMainWindow, Ui_MainWindow2):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.dispTable)

    def dispTable(self):
        print("hi")

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    MainWindow = first() 
    MainWindow.show()
    sys.exit(app.exec_())

在此处输入图像描述

于 2019-03-05T10:21:57.603 回答