我已经设置了登录系统。将状态切换到忘记密码页面或创建帐户页面然后按后退按钮后,主页上的所有按钮都不再响应。
主.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from MainScreenGUI import Ui_MainWindow
from forgotPasswordGUI import ForgotPasswordWindow
from createAccountGUI import CreateAccountWindow
class MyMainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.createAccountButton.clicked.connect(self.show_create_account_window)
self.ui.forgotPasswordButton.clicked.connect(self.show_forgot_password_window)
def show_create_account_window(self):
self.create_account_window = CreateAccountWindow()
self.create_account_window.setupUi(self, self) # Pass the MainWindow instance
self.setCentralWidget(self.create_account_window.centralwidget)
def show_forgot_password_window(self):
self.forgot_password_window = ForgotPasswordWindow(self)
self.forgot_password_window.setupUi(self)
self.setCentralWidget(self.forgot_password_window.centralwidget)
def go_back_to_main_screen(self):
self.ui.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyMainWindow()
window.show()
sys.exit(app.exec_())
忘记密码GUI.py:
from PyQt5 import QtCore, QtGui, QtWidgets
from MainScreenGUI import Ui_MainWindow
class ForgotPasswordWindow(object):
def __init__(self, main_window):
self.main_window = main_window
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1080)
MainWindow.setMinimumSize(QtCore.QSize(1920, 1080))
MainWindow.setMaximumSize(QtCore.QSize(1920, 1080))
MainWindow.setBaseSize(QtCore.QSize(1920, 1080))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
{text box/label code}
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(699, 500, 521, 31))
self.pushButton.setObjectName("pushButton")
self.backButton = QtWidgets.QPushButton(self.centralwidget)
self.backButton.setGeometry(QtCore.QRect(10, 10, 40, 40))
self.backButton.setIcon(QtGui.QIcon("../Images/back_arrow.png"))
self.backButton.setIconSize(QtCore.QSize(30, 30))
self.backButton.setObjectName("backButton")
self.backButton.clicked.connect(self.goBack)
MainWindow.setCentralWidget(self.centralwidget)
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", "Reset Password"))
def goBack(self):
self.main_window.go_back_to_main_screen()
MainScreenGUI.py:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1080)
MainWindow.setMinimumSize(QtCore.QSize(1920, 1080))
MainWindow.setMaximumSize(QtCore.QSize(1920, 1080))
MainWindow.setBaseSize(QtCore.QSize(1920, 1080))
{text box/label code}
self.forgotPasswordButton = QtWidgets.QPushButton(self.centralwidget)
self.forgotPasswordButton.setGeometry(QtCore.QRect(1019, 489, 202, 40))
self.forgotPasswordButton.setObjectName("forgotPasswordButton")
self.createAccountButton = QtWidgets.QPushButton(self.centralwidget)
self.createAccountButton.setGeometry(QtCore.QRect(700, 489, 202, 40))
self.createAccountButton.setObjectName("createAccountButton")
self.loginButton = QtWidgets.QPushButton(self.centralwidget)
self.loginButton.setGeometry(QtCore.QRect(700, 550, 521, 41))
self.loginButton.setObjectName("loginButton")
MainWindow.setCentralWidget(self.centralwidget)
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.textEdit_2.setText(_translate("MainWindow", ""))
self.textEdit.setText(_translate("MainWindow", ""))
self.forgotPasswordButton.setText(_translate("MainWindow", "Forgot Password"))
self.createAccountButton.setText(_translate("MainWindow", "Create an Account"))
self.loginButton.setText(_translate("MainWindow", "Login"))
我尝试更改用户界面的设置方式并重写部分代码,但我仍然遇到相同的错误。
不要修改 Qt Designer 生成的代码,而是创建另一个从适当的小部件继承的类,并使用初始类来填充它。
QStackedWidget 类提供了一堆小部件,其中一次只有一个小部件可见。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
# from MainScreenGUI import Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1080)
# MainWindow.setMinimumSize(QtCore.QSize(1920, 1080))
# MainWindow.setMaximumSize(QtCore.QSize(1920, 1080))
# MainWindow.setBaseSize(QtCore.QSize(1920, 1080))
# {text box/label code}
self.centralwidget = QtWidgets.QWidget()
self.centralwidget.setObjectName("centralwidget")
self.forgotPasswordButton = QtWidgets.QPushButton(self.centralwidget)
self.forgotPasswordButton.setGeometry(QtCore.QRect(1019, 489, 202, 40))
self.forgotPasswordButton.setObjectName("forgotPasswordButton")
self.createAccountButton = QtWidgets.QPushButton(self.centralwidget)
self.createAccountButton.setGeometry(QtCore.QRect(700, 489, 202, 40))
self.createAccountButton.setObjectName("createAccountButton")
self.loginButton = QtWidgets.QPushButton(self.centralwidget)
self.loginButton.setGeometry(QtCore.QRect(700, 550, 521, 41))
self.loginButton.setObjectName("loginButton")
MainWindow.setCentralWidget(self.centralwidget)
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.textEdit_2.setText(_translate("MainWindow", ""))
# ? self.textEdit.setText(_translate("MainWindow", ""))
self.forgotPasswordButton.setText(_translate("MainWindow", "Forgot Password"))
self.createAccountButton.setText(_translate("MainWindow", "Create an Account"))
self.loginButton.setText(_translate("MainWindow", "Login"))
#from forgotPasswordGUI import ForgotPasswordWindow
class ForgotPasswordWindow(object):
# ? def __init__(self, main_window):
# ? self.main_window = main_window
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1080)
# MainWindow.setMinimumSize(QtCore.QSize(1920, 1080))
# MainWindow.setMaximumSize(QtCore.QSize(1920, 1080))
# MainWindow.setBaseSize(QtCore.QSize(1920, 1080))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# {text box/label code}
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(699, 500, 521, 31))
self.pushButton.setObjectName("pushButton")
self.backButton = QtWidgets.QPushButton(self.centralwidget)
self.backButton.setGeometry(QtCore.QRect(10, 10, 40, 40))
self.backButton.setIcon(QtGui.QIcon("Ok.png"))
self.backButton.setIconSize(QtCore.QSize(30, 30))
self.backButton.setObjectName("backButton")
# ? self.backButton.clicked.connect(self.goBack)
MainWindow.setCentralWidget(self.centralwidget)
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", "Reset Password"))
# ? def goBack(self):
# ? self.main_window.go_back_to_main_screen()
# !!! +++
class MyForgotPasswordWindow(QMainWindow, ForgotPasswordWindow): # !!! +++
def __init__(self):
super().__init__()
self.setupUi(self)
#from createAccountGUI import CreateAccountWindow
class CreateAccountWindow(QMainWindow): # !!! +++
def __init__(self):
super().__init__()
self.centralwidget = QtWidgets.QWidget()
self.centralwidget.setObjectName("centralwidget")
self.setCentralWidget(self.centralwidget)
self.label = QtWidgets.QLabel()
self.label.setObjectName("label")
self.label.setText("CreateAccountWindow")
self.label.setStyleSheet('''
#label {
font-size: 30px;
color: #FF4500;
}
''')
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.layout = QtWidgets.QVBoxLayout(self.centralwidget)
self.layout.addWidget(self.label)
self.back_button = QtWidgets.QPushButton(self.centralwidget)
self.back_button.setObjectName("back_button")
self.back_button.setText("<- Back")
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# !!! +++
class MainWindow(QMainWindow): # !!! +++
def __init__(self):
super().__init__()
self.centralwidget = QtWidgets.QWidget()
self.centralwidget.setObjectName("centralwidget")
self.setCentralWidget(self.centralwidget)
self.myMainWindow = MyMainWindow() # !!! +++
self.createAccountWindow = CreateAccountWindow() # !!! +++
self.myForgotPasswordWindow = MyForgotPasswordWindow() # !!! +++
self.stack = QtWidgets.QStackedWidget(self) # !!! +++
self.ix_stack_1 = self.stack.addWidget(self.myMainWindow)
self.ix_stack_2 = self.stack.addWidget(self.createAccountWindow)
self.ix_stack_3 = self.stack.addWidget(self.myForgotPasswordWindow)
self.myMainWindow.createAccountButton.clicked.connect(
lambda: self.stack.setCurrentIndex(1))
self.myMainWindow.forgotPasswordButton.clicked.connect(
lambda: self.stack.setCurrentIndex(2))
self.createAccountWindow.back_button.clicked.connect( # +++
lambda : self.stack.setCurrentIndex(0))
self.myForgotPasswordWindow.backButton.clicked.connect( # +++
lambda : self.stack.setCurrentIndex(0))
self.QVBlayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.QVBlayout.addWidget(self.stack)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(1920, 1080)
window.show()
sys.exit(app.exec_())