Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have designed two screens in PyQt5 It is runnig fine on startup every button is working. But when I click back button on second screen that is Ui_Dialog to go to first screen then on first screen If I click again any button It gives error that Ui_Dialog is not defined which is previously working fine kindly check my back Button code I have attached both screens code snippet.

Screen 1 MainWindow

from PyQt5 import QtCore, QtGui, QtWidgets
from Login import *


class Ui_MainWindow(object):
    def adminLogin(self):
        try:
            self.w = QtWidgets.QDialog()  
            self.ui = Ui_Dialog()  
            self.ui.setupUi(self.w) 
            Ui_Dialog.CurrentUser = "admin"  
            self.w.show()  
        except Exception as e:
            print(e)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(572, 468)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        .
        .
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.btnDoctor.clicked.connect(self.adminLogin)
        self.btnDoctor.clicked.connect(MainWindow.close)

        self.btnPatient.clicked.connect(self.patLogin)
        self.btnPatient.clicked.connect(MainWindow.hide)

        self.btnExit.clicked.connect(QtWidgets.qApp.quit)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.btnDoctor.setText(_translate("MainWindow", "Doctor"))
        self.btnPatient.setText(_translate("MainWindow", "Patient"))
        self.btnExit.setText(_translate("MainWindow", "Exit"))
        self.nameLabel.setText(_translate("MainWindow", "Hospital Management System"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()


screen 2 Ui_Dialog

from PyQt5 import QtCore, QtGui, QtWidgets
import pymysql
from mainWindow import *
from AdminLogin import *
from Register import *
from PatientLogin import *

class Ui_Dialog(object):
    CurrentUser = ""

    def messageBox(self,title,message):
        msg=QtWidgets.QMessageBox()
        msg.setWindowTitle(title)
        msg.setText(message)
        msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
        msg.exec_()

    def loginUser(self):
        try:
            username = self.user_txt.toPlainText()
            password = self.pass_txt.toPlainText()

            if username!="" and password!="":
                conn = pymysql.connect(host="localhost", user="root", password="", db="Hospital_Management_System")
                cur = conn.cursor()
                if self.CurrentUser=="admin":
                    query = "select * from admin where name=%s and password=%s"
                    data = cur.execute(query, (username, password))
                    if len(cur.fetchall()) > 0:
                        self.messageBox("Alert", "Welcome " + self.user_txt.toPlainText())
                        self.w = QtWidgets.QDialog()  # type of window you want to open
                        self.ui = Ui_adminLogin()  # object of window you want to open
                        self.ui.setupUi(self.w)  # initialize the object of window you want to open
                        self.w.show()  # show the required window
                        Ui_adminLogin.CurrentUser=username

                    else:
                        self.messageBox("Alert", "Wrong Credentials")

            else:
              self.messageBox("Alert","Please Fill all Credentials")

        except Exception as e:
            print(e)
    def back(self):
        try:
            self.w = QtWidgets.QMainWindow()  # type of window you want to open
            self.ui = Ui_MainWindow() # object of window you want to open
            self.ui.setupUi(self.w)  # initialize the object of window you want to open
            self.w.show()  # show the required window
        except Exception as e:
            print(e)

    def reg(self):
        if self.CurrentUser=="patient":
            self.w=QtWidgets.QDialog()
            self.ui=Ui_Register()
            self.ui.setupUi(self.w)
            self.w.show()
        
    def setupUi(self, Dialog):
        Dialog.setObjectName("MainWindow")
        Dialog.resize(513, 299)
        self.Username = QtWidgets.QLabel(Dialog)
        self.Username.setGeometry(QtCore.QRect(150, 70, 61, 21))
        self.Username.setObjectName("Username")
        self.Password = QtWidgets.QLabel(Dialog)
        self.Password.setGeometry(QtCore.QRect(150, 120, 47, 13))
        self.Password.setObjectName("Password")
        .
        .
        self.btnBack.setGeometry(QtCore.QRect(430, 10, 75, 23))
        self.btnBack.setObjectName("btnBack")
        self.loginButton.clicked.connect(self.loginUser)
        self.loginButton.clicked.connect(Dialog.hide)

        self.registerButoon.clicked.connect(self.reg)
        self.registerButoon.clicked.connect(Dialog.hide)

        self.btnBack.clicked.connect(self.back)
        self.btnBack.clicked.connect(Dialog.hide)

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.Username.setText(_translate("Dialog", "Username "))
        self.Password.setText(_translate("Dialog", "Password"))
        self.Forgot_Password.setText(_translate("Dialog", "Forgot Password?"))
        self.loginButton.setText(_translate("Dialog", "Login"))
        self.registerButoon.setText(_translate("Dialog", "Register"))
        self.btnBack.setText(_translate("Dialog", "Back"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())


What I have tried:

Every back button code available has same code like this
Posted
Comments
Richard MacCutchan 29-Mar-21 3:51am    
You appear to have a closed loop in your code. Your main window creates the dialog and calls show. Your dialog window then creates a main window and calls show. This could go on for ever.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900