python pyqt5中的登录身份验证错误以及如何在标签中设置实例变量?



如果用户名和密码错误的部分代码,我在运行时遇到问题。 以及如何在 admin_object = userFullname.setText(( 中将索引类的用户全名设置为 lable?

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_())

我的基础不是那么强,请指导我。

你必须改进你的逻辑,而不是选择返回USER_EMAIL必须返回USER_FULLNAME这样你就可以避免进行新的查询,另一方面要得到你必须使用的结果fetchall(),这将返回结果的元组,如果没有结果将为空,否则会有结果。在您的情况下,我假设您将USER_EMAIL限制为唯一,以便结果将是单个元素的元组。该数据保存在类的一个成员中,并在另一个窗口中设置它。

另一方面,admin_operation不必从索引继承,因此请将其删除。


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_())

最新更新