Issue
I've setup a login system. After switching states to either the forgotPassword page or the create account page then pressing the backbutton, all buttons on the mainpage are no longer responsive. The
main.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_())
forgotPasswordGUI.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"))
Ive tried changing how the ui is setup and rewritten parts of the code but i still get the same error.
Solution
Do not modify the code generated by Qt Designer but create another class that inherits from the appropriate widget and use the initial class to fill it.
QStackedWidget Class
The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.
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_())
Answered By - S. Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.