Login authentication error in python pyqt5 and how

2019-08-22 19:21发布

问题:

I am having trouble running if part of code where user name and password is wrong.program does not show any error. And how can i set userfullname of Index class to lable in admin_object = userFullname.setText() ?

import sqlite3

from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QMessageBox

from Login_form import Login_form
from Admin_Home_form import AdminHome_Form


class Index(QtWidgets.QDialog, Login_form):
    def __init__(self):
        QtWidgets.QDialog.__init__(self)
        self.loginFormDesign(self)
        self.login_button.clicked.connect(self.login_check)

    def login_check(self):
        uname = self.U_name_text.text()
        password = self.pass_text.text()
        # self.userfullname = userfullname
        connection = sqlite3.connect("taylorDB.db")
        result = connection.execute("SELECT USER_EMAIL FROM USERS WHERE USER_EMAIL = ? AND USER_PASSWORD = ?",
                                (uname, password))
        for dbemail in result:  # After this code does not work if value does not match
            print(dbemail[0])
            if dbemail[0] != uname:
                buttonReply = QMessageBox.question(self, 'Login Invalid', "Check User Name or Password!",
                                               QMessageBox.Close)
                if buttonReply == QMessageBox.Close:
                    print("invalid login")
            else:
                result = connection.execute("SELECT USER_FULLNAME FROM USERS WHERE USER_EMAIL = ?", (dbemail))
                self.userfullname = result.fetchone()
                print(self.userfullname)
                self.accept()


class admin_operation(QtWidgets.QMainWindow, AdminHome_Form, Index):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.adminHomeFormDesign(self)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    login_object = Index()
    admin_object = admin_operation()

    if login_object.exec() == QtWidgets.QDialog.Accepted:
        # admin_object.userFullname.setText()   #I want to set the value of userfullname here?
        admin_object.show()

sys.exit(app.exec_())

My basic are not so strong please guide me .

回答1:

You must improve your logic, instead of the select return USER_EMAIL must return the USER_FULLNAME so you avoid making a new query, on the other hand to get the results you must use fetchall(), this will return a tuple of the results, if no results will be empty, otherwise there will be results. In your case, I assume that you restrict the USER_EMAIL to being unique so that the result will be a tuple of a single element. That data you keep in one member of the class, and you set it in the other window.

On the other hand it is not necessary that admin_operation inherit from Index, so delete it.


import sqlite3

from PyQt5.QtWidgets import QMessageBox, QDialog, QMainWindow, QApplication

from Login_form import Login_form
from Admin_Home_form import AdminHome_Form


class Index(QDialog, Login_form):
    def __init__(self):
        QDialog.__init__(self)
        self.loginFormDesign(self)
        self.login_button.clicked.connect(self.login_check)

    def login_check(self):
        uname = self.U_name_text.text()
        password = self.pass_text.text()
        connection = sqlite3.connect("taylorDB.db")
        result = connection.execute("SELECT USER_FULLNAME FROM USERS WHERE USER_EMAIL = ? AND USER_PASSWORD = ?",
                                    (uname, password)).fetchall()

        if result:
            self.userfullname = result[0][0]
            self.accept()
        else:
            buttonReply = QMessageBox.question(self, 'Login Invalid', "Check User Name or Password!",
                                                   QMessageBox.Close)
            if buttonReply == QMessageBox.Close:
                print("invalid login")
                # self.reject()


class Admin_operation(QMainWindow, AdminHome_Form):
    def __init__(self):
        QMainWindow.__init__(self)
        self.adminHomeFormDesign(self)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    login_object = Index()
    admin_object = Admin_operation()

    if login_object.exec() == QDialog.Accepted:
        admin_object.userFullname.setText(login_object.userfullname)
        admin_object.show()

    sys.exit(app.exec_())