我的代码中首先有两个窗口,我想打开包含btn1的class first()窗口。当我单击btn1时,我想打开一个新窗口并将其替换为以前的窗口(即在当前窗口本身中打开新窗口),因此在单击btn1时,将显示包含btn2的class 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工具创建的类。
答案 0 :(得分: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_())