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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章