Python Forum
[PyQt] [Solved]Help Passing Variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Help Passing Variables
#1
Hello,

I'm wondering how I can pass my userInputName and userInputPassword to my validate function.
I thought this was the correct way of doing it (because that's how I did it in my terminal version) but apparently it's not because I don't get the Success print messages from the validate function.

For example: If I type in Admin as the Name, and 1234 as the Password. I get:
Output:
The Login Button was clicked Name: Admin Password: 1234
But no: Success! You are now logged in as: Admin, message

What am I doing wrong, and how do I fix it?

Thanks in advance.


A Chunk of my LoginScreen.py (My LoginScreen GUI Code):

#----------------------------------
    #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)
        #Go to validate function
        validate(userInputName, userInputPassword)
#----------------------------------
#---------------------------------------------------------------
#                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()
        return True

    if SuperUsers().get(userInputName) == userInputPassword:
        print('Success! You are now logged in as: ' + userInputName)
        #Switch from this csreen to the AdminMenu Screen
        #adminMenu()
        
    else:
        return
#---------------------------------------------------------------
My UserDatabase.py:
#-------------------------------------------------------------------------------------
                                #UserDatabase.py 
#-------------------------------------------------------------------------------------
#This is where all valid users are stored
#-------------------------------------------------------------------------------------
def StandardUsers():
    users = {
        "Bob" : 1234,
        "Jim" : 5678
    }
    return users

def SuperUsers():
    admins = {
        "Admin" : 1234,
        "Joe" : 0000,
        "Randall" : 1111
    }
    return admins
Full code (If Interested):
from PyQt5 import QtCore, QtGui, QtWidgets

from UserDatabase import StandardUsers, SuperUsers

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"
"Developed By: Paglia Industries\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)
        #Go to validate function
        validate(userInputName, userInputPassword)
#----------------------------------
#---------------------------------------------------------------
#                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()
        return True

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

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
#2
You should not write a function that returns True or None. None is falsy, but if a function can return True, the only other return value should be False.

It looks like your dictionaries contain numbers for the password and you are passing a string. "1234" does not equal 1234
Reply
#3
(May-12-2022, 08:29 PM)deanhystad Wrote: You should not write a function that returns True or None. None is falsy, but if a function can return True, the only other return value should be False.

It looks like your dictionaries contain numbers for the password and you are passing a string. "1234" does not equal 1234

How do I convert the string to an int then? Do I have to do something fancy since it's PyQt or do I just go int(userInputPassword)?
Reply
#4
You can convert a str to an int.
userInputPassword = int(self.PasswordInput.text())
But now you have to worry about the user typing something that cannot be converted to an int, like "cat" or even "1.0". To be safe you should wrap the dangerous conversion inside a try except.
try:
    userInputPassword = self.PasswordInput.text()
except ValueError:
    #Do something here to handle the error
But this adds a lot of complication to the code. And for what? Why do you want to convert the str to an int? Are you going to do math with the passwords? Does the numerical value of the password have some significance? Why not make the passwords all str?
    admins = {
        "Admin" : "1234",
        "Joe" : "0000",
        "Randall" : "1111"
    }
If there is no reason for it t be a number, and it is more convenient for it to be a str, let it be a str.
Reply
#5
(May-12-2022, 09:02 PM)deanhystad Wrote: But this adds a lot of complication to the code. And for what? Why do you want to convert the str to an int? Are you going to do math with the passwords? Does the numerical value of the password have some significance? Why not make the passwords all str?

That's a very good point, and I have no clue why I didn't think of that before. I guess every time time I see a number I just assume Int or Double because I usually do calculations with them, of course this isn't the case for the password.

Thanks for the help and I can't believe I didn't think of that before.
Reply


Forum Jump:

User Panel Messages

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