Python 简单登录功能的实现eazygui

pymysql_gui.py

# 导入pymysql模块
import pymysql
def select_mysql(zhanghao):
   # 连接database
   conn = pymysql.connect(host="localhost", user="root",password="root1",database="test",charset="utf8")
   # 得到一个可以执行SQL语句的光标对象
   cursor = conn.cursor()
   sql = "SELECT zhanghao,keyword from users WHERE zhanghao="+repr(zhanghao)+";"
   # 执行SQL语句
   cursor.execute(sql)
   # 获取单条查询数据
   ret = cursor.fetchone()
   cursor.close()
   conn.close()
   # 打印下查询结果
   return ret

def insert_mysql(zhanghao,keyword,gjj):
   # 连接database
   conn = pymysql.connect(host="localhost", user="root", password="root1", database="test", charset="utf8")
   # 得到一个可以执行SQL语句的光标对象
   cursor = conn.cursor()
   sql = 'insert into users(zhanghao,keyword,gjj) values (%s,%s,%s);'
   # 执行SQL语句
   data = (repr(zhanghao),repr(keyword),repr(gjj))
   try:
      cursor.execute(sql % data)
      conn.commit()
      return True
   except:
      return False
   cursor.close()
   conn.close()

"""
author:魏振东
data:2019.09.29
func:简单登录功能的实现
"""
import easygui as g
import pymysql_gui as pmsg
import random

#登录
def login():
    # 生成验证算式
    i = random.randint(0, 10)
    j = random.randint(0, 10)

    msg = "请输入用户名和密码\n验证码:%s*%s=?" % (i, j)
    title = "用户登录"
    user_info = g.multpasswordbox(msg, title, ("用户名", "密码", "验证码"))
    try:
        # 验证数据库,密码,验证码
        if user_info[0] == pmsg.select_mysql(user_info[0])[0] and user_info[1] == pmsg.select_mysql(user_info[0])[1] and int(user_info[2]) == i * j:
            g.msgbox("登录成功!")
        else:
            g.msgbox("账号或者密码错误,登录失败!请重新输入")
            login()
    except:
        g.msgbox("登录异常!!!")

# 注册
def register():
    msg = "请输入用户名和密码"
    title = "注册"
    user_info = g.multpasswordbox(msg, title, ("用户名", "密码"))
    # 验证账号是否已被注册
    if pmsg.insert_mysql(user_info[0],user_info[1],"0"):
        g.msgbox("注册成功!")
        login()
    else:
        g.msgbox("注册失败!")
        register()


# 登录或注册
msg = "注册或已有账号直接登录?"
choices = g.buttonbox(msg,choices=("登录","注册"))
if choices == "登录":
    login()
else:
    register()
发布了39 篇原创文章 · 获赞 41 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wei_zhen_dong/article/details/102649692