Получение кнопки щелчка в PyQt5 для запуска скрипта проверки пароля - PullRequest
0 голосов
/ 09 мая 2020

Я пишу приложение PyQt5 с системой входа в систему, которая работает с SQLite3. Упомянутая система входа в систему закодирована и работает, и теперь мне нужно интегрировать ее с GUI. Однако у меня возникают проблемы с запуском кода - в частности, с запуском кода входа в систему, когда я нажимаю кнопку «ОК» на экране входа в систему. Ниже приведен код:

class OpeningScreen(QtWidgets.QMainWindow, Ui_splashScreen):
    def __init__(self, parent=None):
        super(OpeningScreen, self).__init__(parent)
        self.setupUi(self)  # sets up the user interface from PyQt Designer

class LoginUsers(QtWidgets.QMainWindow, Ui_loginScreen):
    def __init__(self, parent=None):
        super(LoginUsers, self).__init__(parent)
        self.setupUi(self)
        self.pushButton_2.clicked.connect(self.login)
        #self.pushButton_3.clicked.connect(LoginUsers.closed.connect())

    def login(self):  # the module which allows existing users to login
        while True:
            username_value = self.username_input.toPlainText()
            username = username_value
            #username = input("Please enter your username: ")  # asks the user to enter their username
            password_value = self.password_input.toPlainText()
            password = password_value
            #password = input("Please enter your password: ")  # asks the user to enter their password
            with sqlite3.connect("C:\sqlite\db\SUTHATusers.db") as db:
                # connects to the database 'SUTHATusers.db', linking to its file path on the hard drive
                cursor = db.cursor()  # cursor allows the database to be traversed
            cursor.execute("SELECT password FROM users WHERE username = ?", (username,))
            # cursor executes the above SQL command
            stored_password = cursor.fetchone()[0]  # returns the results of the SQL command

            def verify_password(stored_password, password):
                # This module takes an encoded password from the database and the plaintext one as entered by the user
                # it verifies whether the password entered by the user matches the one saved in the database
                salt = stored_password[:64]
                # extracts the salt of the stored password in the database by retrieving its first 64 characters
                stored_password = stored_password[64:]
                # sets the stored password to be all but the last 64 characters of the value retrieved from the database
                pwdhash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt.encode('ascii'), 100000)
                # computes the hash of the plaintext password and the extracted salt
                pwdhash = binascii.hexlify(pwdhash).decode('ascii')
                # converts the hash to an ASCII string
                return (pwdhash == stored_password)
                # assigns the hash as the value the stored password

            if verify_password(stored_password, password):
                # checks the encoded password entered by the user against its hash
                # if they match, the user is logged in
                print("Welcome " + username)  # prints a welcome message and the username
                break  # ends the if statement


            else:
                # if no passwords are found or the password entered is incorrect, the below code is run
                print("Username and password not recognized ")
                # message telling the user that their details have not been recognised
                again = input("Do you want to try again? y/n: ")  # asks the user if they want to re-enter their details
                if again == "n":  # if the user chooses not to re-enter their details, the program restarts
                    print("Bye bye")  # goodbye message is displayed to the user
                    time.sleep(1)  # program pauses for one second
                    sys.exit()
                    # break  # if statement ends

Я не знаю, где я ошибаюсь, но подозреваю, что это что-то обманчиво простое. Я новичок в PyQt5, поэтому мне пришлось немного учиться. Большое спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...