Python Forum
[PyQt] [Solved]Help Getting Started on Scene Swapping
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Help Getting Started on Scene Swapping
#1
Hello,

I have yet another question, but this one entails replacing the current screen/window with a new one when it is called.

I'm trying to have my AdminMenu.py(A PyQT GUI) open on the same windows after the user has logged in on the LoginScreen.py(A PyQT GUI).

How can I achieve this?
Do I just call the class Ui_MainDisplay(object):?

I looked online but I can't find many tutorials on this, so any help is greatly appreciated.

Thanks in advance.


The validate chunk of my Login Screen Code which will take the user to the appropriate menu (I'm only focusing on adminMenu for now):
#---------------------------------------------------------------
#                Validate Function -> Validates user 
#---------------------------------------------------------------
#Pass in user and password variables from login function
#Check if the user is valid (registered in database) -> Give access to inventory program 
#If user is not valid -> return to login
#-->TODO: Write the user and current date to a file to keep track of logins
def validate(userInputName, userInputPassword):
    if StandardUsers().get(userInputName) == userInputPassword:
        print('Success! You are now logged in as: ' + userInputName)
        #Switch from this screen to the MainMenu Screen
        #mainMenu()

    if SuperUsers().get(userInputName) == userInputPassword:
        print('Success! You are now logged in as: ' + userInputName)
        #Switch from this csreen to the AdminMenu Screen
        #adminMenu()
        
    else:
        print('You are not registered to use this program')
#---------------------------------------------------------------



My AdminMenu screen code (Right now):
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainDisplay(object):
    def setupUi(self, MainDisplay):
        MainDisplay.setObjectName("MainDisplay")
        MainDisplay.resize(1123, 903)
        MainDisplay.setStyleSheet("background-color: rgb(0, 170, 255);")
        self.centralwidget = QtWidgets.QWidget(MainDisplay)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setContentsMargins(-1, 0, -1, 0)
        self.verticalLayout.setSpacing(6)
        self.verticalLayout.setObjectName("verticalLayout")
        self.AddItemButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.AddItemButton.setFont(font)
        self.AddItemButton.setStyleSheet("background-color: rgb(85, 255, 0);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"\n"
"")
        self.AddItemButton.setObjectName("AddItemButton")
        self.verticalLayout.addWidget(self.AddItemButton)
        self.DeleteItemButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.DeleteItemButton.setFont(font)
        self.DeleteItemButton.setStyleSheet("background-color: rgb(255, 65, 68);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.DeleteItemButton.setObjectName("DeleteItemButton")
        self.verticalLayout.addWidget(self.DeleteItemButton)
        self.CheckoutButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.CheckoutButton.setFont(font)
        self.CheckoutButton.setStyleSheet("background-color: rgb(255, 255, 0);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.CheckoutButton.setObjectName("CheckoutButton")
        self.verticalLayout.addWidget(self.CheckoutButton)
        self.ReturnButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.ReturnButton.setFont(font)
        self.ReturnButton.setStyleSheet("background-color: rgb(255, 170, 32);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.ReturnButton.setObjectName("ReturnButton")
        self.verticalLayout.addWidget(self.ReturnButton)
        self.ScanBarcodeButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.ScanBarcodeButton.setFont(font)
        self.ScanBarcodeButton.setStyleSheet("background-color: rgb(211, 211, 211);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.ScanBarcodeButton.setObjectName("ScanBarcodeButton")
        self.verticalLayout.addWidget(self.ScanBarcodeButton)
        self.MoreInfoButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.MoreInfoButton.setFont(font)
        self.MoreInfoButton.setStyleSheet("background-color: rgb(196, 17, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;")
        self.MoreInfoButton.setObjectName("MoreInfoButton")
        self.verticalLayout.addWidget(self.MoreInfoButton)
        self.RefreshButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.RefreshButton.setFont(font)
        self.RefreshButton.setStyleSheet("background-color: rgb(0, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"\n"
"")
        self.RefreshButton.setObjectName("RefreshButton")
        self.verticalLayout.addWidget(self.RefreshButton)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.LogoutButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.LogoutButton.setFont(font)
        self.LogoutButton.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.LogoutButton.setObjectName("LogoutButton")
        self.verticalLayout.addWidget(self.LogoutButton)
        self.gridLayout.addLayout(self.verticalLayout, 4, 3, 1, 1)
        self.Header = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.Header.setFont(font)
        self.Header.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;")
        self.Header.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        self.Header.setObjectName("Header")
        self.gridLayout.addWidget(self.Header, 0, 1, 1, 3)
        self.SearchButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.SearchButton.setFont(font)
        self.SearchButton.setStyleSheet("background-color: rgb(211, 211, 211);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.SearchButton.setObjectName("SearchButton")
        self.gridLayout.addWidget(self.SearchButton, 2, 1, 1, 1)
        self.SearchBar = QtWidgets.QLineEdit(self.centralwidget)
        self.SearchBar.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.SearchBar.setObjectName("SearchBar")
        self.gridLayout.addWidget(self.SearchBar, 2, 2, 1, 1)
        self.InventoryDisplay = QtWidgets.QTableWidget(self.centralwidget)
        font = QtGui.QFont()
        font.setBold(False)
        font.setWeight(50)
        self.InventoryDisplay.setFont(font)
        self.InventoryDisplay.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"")
        self.InventoryDisplay.setObjectName("InventoryDisplay")
        self.InventoryDisplay.setColumnCount(11)
        self.InventoryDisplay.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(5, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(6, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(7, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(8, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(9, item)
        item = QtWidgets.QTableWidgetItem()
        self.InventoryDisplay.setHorizontalHeaderItem(10, item)
        self.gridLayout.addWidget(self.InventoryDisplay, 4, 1, 1, 2)
        MainDisplay.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainDisplay)
        self.statusbar.setObjectName("statusbar")
        MainDisplay.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainDisplay):
        _translate = QtCore.QCoreApplication.translate
        MainDisplay.setWindowTitle(_translate("MainDisplay", "MainWindow"))
        self.AddItemButton.setText(_translate("MainDisplay", "Add Item"))
        self.DeleteItemButton.setText(_translate("MainDisplay", "Delete Item"))
        self.CheckoutButton.setText(_translate("MainDisplay", "Check Out"))
        self.ReturnButton.setText(_translate("MainDisplay", "Return"))
        self.ScanBarcodeButton.setText(_translate("MainDisplay", "Scan Barcode"))
        self.MoreInfoButton.setText(_translate("MainDisplay", "More Info"))
        self.RefreshButton.setText(_translate("MainDisplay", "Refresh"))
        self.LogoutButton.setText(_translate("MainDisplay", "Log Out"))
        self.Header.setText(_translate("MainDisplay", "Admin Menu"))
        self.SearchButton.setText(_translate("MainDisplay", "Search"))
        item = self.InventoryDisplay.horizontalHeaderItem(0)
        item.setText(_translate("MainDisplay", "ID"))
        item = self.InventoryDisplay.horizontalHeaderItem(1)
        item.setText(_translate("MainDisplay", "Name"))
        item = self.InventoryDisplay.horizontalHeaderItem(2)
        item.setText(_translate("MainDisplay", "Quantity"))
        item = self.InventoryDisplay.horizontalHeaderItem(3)
        item.setText(_translate("MainDisplay", "Price ($)"))
        item = self.InventoryDisplay.horizontalHeaderItem(4)
        item.setText(_translate("MainDisplay", "Sell Price ($)"))
        item = self.InventoryDisplay.horizontalHeaderItem(5)
        item.setText(_translate("MainDisplay", "Description"))
        item = self.InventoryDisplay.horizontalHeaderItem(6)
        item.setText(_translate("MainDisplay", "Category"))
        item = self.InventoryDisplay.horizontalHeaderItem(7)
        item.setText(_translate("MainDisplay", "Location"))
        item = self.InventoryDisplay.horizontalHeaderItem(8)
        item.setText(_translate("MainDisplay", "Length (Ft)"))
        item = self.InventoryDisplay.horizontalHeaderItem(9)
        item.setText(_translate("MainDisplay", "Barcode #"))
        item = self.InventoryDisplay.horizontalHeaderItem(10)
        item.setText(_translate("MainDisplay", "Date Updated"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainDisplay = QtWidgets.QMainWindow()
    ui = Ui_MainDisplay()
    ui.setupUi(MainDisplay)
    MainDisplay.show()
    sys.exit(app.exec())
Updated: 05/14/22 to show the right code. I accidently posted the AdminMenu code twice
The full Login Screen Code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox

from DataBases.UserDatabase import StandardUsers, SuperUsers

from AdminMenu import Ui_MainDisplay

class Ui_Loginscreen(object):
    def setupUi(self, Loginscreen):
        Loginscreen.setObjectName("Loginscreen")
        Loginscreen.resize(1108, 895)
        Loginscreen.setStyleSheet("background-color: rgb(0, 170, 255);")
        self.centralwidget = QtWidgets.QWidget(Loginscreen)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.Header = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.Header.setFont(font)
        self.Header.setAlignment(QtCore.Qt.AlignCenter)
        self.Header.setObjectName("Header")
        self.gridLayout.addWidget(self.Header, 0, 0, 1, 1)
        self.formLayout_2 = QtWidgets.QFormLayout()
        self.formLayout_2.setObjectName("formLayout_2")
        self.NameLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.NameLabel.setFont(font)
        self.NameLabel.setObjectName("NameLabel")
        self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.NameLabel)
        self.NameInput = QtWidgets.QLineEdit(self.centralwidget)
        self.NameInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.NameInput.setText("")
        self.NameInput.setObjectName("NameInput")
        self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.NameInput)
        self.PasswordLabel = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.PasswordLabel.setFont(font)
        self.PasswordLabel.setObjectName("PasswordLabel")
        self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.PasswordLabel)
        self.PasswordInput = QtWidgets.QLineEdit(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily("Wingdings")
        self.PasswordInput.setFont(font)
        self.PasswordInput.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.PasswordInput.setText("")
        self.PasswordInput.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
        self.PasswordInput.setObjectName("PasswordInput")
        self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.PasswordInput)
        self.label = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(8)
        font.setItalic(True)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.formLayout_2.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.label)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.formLayout_2.setItem(3, QtWidgets.QFormLayout.FieldRole, spacerItem)
        self.LoginButton = QtWidgets.QPushButton(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.LoginButton.setFont(font)
        self.LoginButton.setStyleSheet("background-color: rgb(211, 211, 211);\n"
"border-style: outset;\n"
"border-width: 2px;\n"
"border-radius: 15px;\n"
"border-color: black;\n"
"padding: 4px;\n"
"")
        self.LoginButton.setObjectName("LoginButton")
        self.formLayout_2.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.LoginButton)
        self.gridLayout.addLayout(self.formLayout_2, 1, 0, 1, 1)
        Loginscreen.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(Loginscreen)
        self.statusbar.setObjectName("statusbar")
        Loginscreen.setStatusBar(self.statusbar)

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

    def retranslateUi(self, Loginscreen):
        _translate = QtCore.QCoreApplication.translate
        Loginscreen.setWindowTitle(_translate("Loginscreen", "E.I.S"))
        self.Header.setText(_translate("Loginscreen", "Electronic Inventory System (E.I.S)"))
        self.NameLabel.setText(_translate("Loginscreen", "Name"))
        self.PasswordLabel.setText(_translate("Loginscreen", "Password"))
        self.label.setText(_translate("Loginscreen", "\n"
"\n"
"\n"
"\n"
"Electronic Inventory System (E.I.S)\n"
"Version: 2.6\n"
"Last Updated: 05/11/2022\n"
"\n"
"\n"
""))
        self.LoginButton.setText(_translate("Loginscreen", "Log In"))

        #------------------------------------------
                        #Actions
        #------------------------------------------
        #When the Login button is clicked -> LoginClicked Function
        LoginButton = self.LoginButton
        LoginButton.clicked.connect(self.LoginClicked)
        #------------------------------------------
#----------------------------------
    #Store Name and Password
    def LoginClicked(self):
        userInputName = self.NameInput.text()
        userInputPassword = self.PasswordInput.text()
        #Print in terminal for testing:
        print("The Login Button was clicked")
        print("Name: " + userInputName)
        print("Password: " + userInputPassword)

        #Validate Login Info
        if StandardUsers().get(userInputName) == userInputPassword:
            print('Success! You are now logged in as: ' + userInputName)
            #Switch from this screen to the MainMenu Screen
            #mainMenu()

        if SuperUsers().get(userInputName) == userInputPassword:
            print('Success! You are now logged in as: ' + userInputName)
            #Switch from this screen to the AdminMenu Screen
            #adminMenu()
            self.win = Ui_MainDisplay()
            self.win.show()
            self.close()

        else:
            #Call Invalid Credentials Popup
            self.invalidCredentials()
            print('You are not registered to use this program')
#----------------------------------
#-----------------------------------------------------------------
    #Invalid Credentails Popup:
    def invalidCredentials(self):
        msgBox = QMessageBox()
        msgBox.setIcon(QMessageBox.Critical)
        msgBox.setText("You are not registered to use this program")
        msgBox.setWindowTitle("Invalid Credentials")
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.buttonClicked.connect(msgButtonClick)

        returnValue = msgBox.exec()
        if returnValue == QMessageBox.Ok:
            print('OK was clicked')
        
def msgButtonClick(i):
    print("Button clicked is:",i.text())
#-----------------------------------------------------------------


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Loginscreen = QtWidgets.QMainWindow()
    ui = Ui_Loginscreen()
    ui.setupUi(Loginscreen)
    Loginscreen.show()
    sys.exit(app.exec_())
Reply


Messages In This Thread
[Solved]Help Getting Started on Scene Swapping - by Extra - May-12-2022, 11:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020