pyqt(python+qt)实现登录注册功能

  • Post author:
  • Post category:python

本文是以文字与代码结合的方式进行讲解,重点是提供思路

以下是我设计的登录注册界面

在这里插入图片描述在这里插入图片描述

登录与注册的主体代码

	def login(self, userno, password):
        login_user = userno                                                 
        login_passwrod = password

        if login_user != "" and login_passwrod != "" : #判断账号与密码是否为空
            if_userno_exist = mysql.query("select user_no from user where user_no = %s", login_user)  #用sql语句查询输入的账号是否存在
            if len(if_userno_exist) > 0:	#长度>0,则表示存在
                if_matched = mysql.query("select user_no from user where user_no = %s and user_password = %s", login_user,login_passwrod)	#用sql语句判断输入的账号和密码是否匹配
                if len(if_matched) > 0:	#长度>0,则表示匹配            
                    #进入主界面,或进行其他操作..........................
                else:
                    QMessageBox.warning(None, '警告', '密码输入错误!', QMessageBox.Ok)
            else:
                QMessageBox.warning(None, '警告', '该账号不存在!', QMessageBox.Ok)
        else:
            QMessageBox.warning(None, '警告', '有信息未输入!', QMessageBox.Ok)

    def register(self, stu_tea_no, password1, password2, nickname):
        register_stuteano = stu_tea_no
        register_password1 = password1
        register_password2 = password2
        register_nickname = nickname

        if register_stuteano != "" and register_password1 != "" and register_password2 != "" and register_nickname != "":
            if_registered = mysql.query("select * from user where stu_tea_no = %s", register_stuteano)	#我的系统要求数据库中必须要有相应的学号或工号,才能进行注册(不过后来想想觉得有点多此一举)
            if len(if_registered) > 0:  #长度>0,则说明存在该用户
                QMessageBox.warning(None, '警告', '该账号已存在。', QMessageBox.Ok)#提示已经存在,请跳转登录
            else:
                if register_password1 == register_password2: #检查两次密码是否相同
                    stu_existed = mysql.query("select student_no from studentinfo where student_no = %s", register_stuteano)
                    tea_existed = mysql.query("select teacher_no from teacherinfo where teacher_no = %s", register_stuteano)
                    
                    if len(stu_existed)>0 or len(tea_existed)>0: #检查学号/工号是否存在
                        result = mysql.exec("insert into user(user_password, user_nick_name, stu_tea_no) value (%s, %s, %s)", (register_password1, register_nickname, register_stuteano))
                        if result > 0: 
                        	#进行页面跳转或其他操作
                        else:
                            QMessageBox.information(None, '提醒', '注册失败!', QMessageBox.Ok)
                    else: 
                        QMessageBox.warning(None, '警告', '输入的学号或工号不存在!', QMessageBox.Ok)
                else:
                    QMessageBox.warning(None, '警告', '两次密码不相同!', QMessageBox.Ok)
        else:
            QMessageBox.warning(None, '警告', '有注册信息未输入!', QMessageBox.Ok)  

以上涉及到了sql查询,因此也顺便展示一下我的mysql.py文件

# -*- coding: utf-8 -*-

import pymysql # 导入操作MySQL数据库的模块

userName="" # 记录用户名

# 打开数据库连接
def open():
    db = pymysql.connect(host="应该都是localhost", user="你的用户名", password="你的密码", database="你的数据库名称", charset="你的字符集")
    return db # 返回连接对象

# 执行数据库的增、删、改操作
def exec(sql,values):
    db=open() # 连接数据库
    cursor = db.cursor() # 使用cursor()方法获取操作游标
    try:
        cursor.execute(sql,values) # 执行增删改的SQL语句
        db.commit() # 提交数据
        return 1 # 执行成功
    except:
        db.rollback() # 发生错误时回滚
        return 0 # 执行失败
    finally:
        cursor.close() # 关闭游标
        db.close() # 关闭数据库连接

# 带参数的精确查询
def query(sql,*keys):
    db=open() # 连接数据库
    cursor = db.cursor() # 使用cursor()方法获取操作游标
    cursor.execute(sql,keys) # 执行查询SQL语句
    result = cursor.fetchall() # 记录查询结果
    cursor.close() # 关闭游标
    db.close() # 关闭数据库连接
    return result # 返回查询结果

# 不带参数的模糊查询
def query2(sql):
    db=open() # 连接数据库
    cursor = db.cursor() # 使用cursor()方法获取操作游标
    cursor.execute(sql) # 执行查询SQL语句
    result = cursor.fetchall() # 记录查询结果
    cursor.close() # 关闭游标
    db.close() # 关闭数据库连接
    return result # 返回查询结果

版权声明:本文为boluobao222原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。