gpt4 book ai didi

python - 在 pyqt 中,按钮单击连接在新窗口中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 21:12:01 26 4
gpt4 key购买 nike

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

最佳答案

尝试一下:

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

enter image description here

关于python - 在 pyqt 中,按钮单击连接在新窗口中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54999282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com