登录表单不工作!python

2024-09-27 09:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在此登录表单中,仅适用于相同的“用户名”和“密码”!我想它的工作,如果任何'用户名'得到匹配str从'密码'名单,那么它也必须工作。。救命啊。。!!!你知道吗

        self.usernamelist = ['aniruddh','firoz','ashish']
        self.passwordlist = ['aniruddh','firoz','ashish']

        self.connect(self.okbutton, SIGNAL("clicked()"),self.loginfunction)


    def loginfunction(self):
        usernamestatus = False
        usernameindex =  -1
        passwordstatus = False
        passwordindex =  -1
        for currentusername in range(len(self.usernamelist)):
            if self.passwordlist[currentusername] == self.username.text():
                usernamestatus = True
                usernameindex = self.usernamelist.index(self.passwordlist[currentusername])

        for currentpassword in range(len(self.passwordlist)):
            if self.usernamelist[currentpassword] == self.password.text():
                passwordstatus = True
                passwordindex = self.passwordlist.index(self.usernamelist[currentpassword])

        if usernamestatus == True and passwordstatus ==True and usernameindex: #== passwordindex:
            self.hide()
            w2 = chooseoption.Form1(self)
            w2.show()


        else:

                        self.msgBox = QMessageBox()
                        self.msgBox.setWindowTitle("Alert!")
                        self.msgBox.setWindowIcon(QtGui.QIcon('abcd.ico'))
                        self.msgBox.setText("Unauthorised User!!!")
            self.msgBox.exec_()

Tags: selftrue密码if用户名msgboxpasswordlistusernamelist
1条回答
网友
1楼 · 发布于 2024-09-27 09:27:06

问题是,当您检查密码时,您正在检查密码是否在用户名列表中!您需要做的是:
1-检查列表中是否存在用户名。
2-检查密码是否对该用户名有效,在您的情况下,passwordlist中索引[i]上的密码对usernamelist中同一索引上的用户名有效。你知道吗

所以登录函数可以是:

def loginfunction(self):
    usernameindex =  -1
    passwordindex =  -1
    username = self.username.text()
    password = self.password.text()
    if username in self.usernamelist: # 1- check: if the username is in the list
        usernameindex = self.usernamelist.index(username) # then: get the index of the username
        if password == self.passwordlist[usernameindex]: # 2- check: if password is equal to the password in passwordlist on the same index of the username
            self.hide()
            w2 = chooseoption.Form1(self)
            w2.show()
        else: # if the password is not correct
            self.msgBox = QMessageBox()
            self.msgBox.setWindowTitle("Alert!")
            self.msgBox.setWindowIcon(QtGui.QIcon('abcd.ico'))
            self.msgBox.setText("Incorrect password!!!")

    else: # the username is not in the list
        self.msgBox = QMessageBox()
        self.msgBox.setWindowTitle("Alert!")
        self.msgBox.setWindowIcon(QtGui.QIcon('abcd.ico'))
        self.msgBox.setText("Unauthorised User!!!")

相关问题 更多 >

    热门问题