Python tkinter 實現簡單登陸注冊 基於B/S三層體系結構,實現用戶身份驗證


Python tkinter 實現簡單登陸注冊

最終效果

  1. 開始界面

  2. 注冊

  3. 登陸


源碼

login.py

# encoding=utf-8
from tkinter import *
from tkinter import messagebox as tkMessageBox
import mysql_connect as mys
import pymysql
import base64

#加密 將用戶的密碼加密后儲存到數據庫
def encryption(str):
    str=str.encode(encoding="utf-8")
    s2=base64.b64encode(str)
    return s2.decode()

#解密 將數據庫返回的密文解密后驗證用戶密碼
def decryption(str):
    str = str.encode(encoding="utf-8")
    s1=base64.b64decode(str)
    return s1.decode()

#處理注冊
def newuser(name,pwd):
    #聲明全局變量,傳遞給insert
    global cursor,db
    pwd=encryption(pwd)   #加密
    if mys.name_exist(cursor,name): #返回正確的值說明用戶名存在
            tkMessageBox.showinfo(title='失敗', message='用戶名已存在!')
            return False
    else:
        mys.insert_data(db=db,cursor=cursor,name=name,password=pwd)
        tkMessageBox.showinfo(title='成功', message='注冊成功')

#處理直接登錄
def olduser(name,pwd):
    global cursor
    # print(name,pwd)
    password=mys.get_password(cursor,name)
    password=decryption(password) #解密
    print("password : ",password)
    print(pwd)
    if password == pwd:
        tkMessageBox.showinfo(title='成功', message='登陸成功! welcome back '+name)
    else:
        tkMessageBox.showinfo(title='失敗', message='登錄失敗')

#處理注冊窗口
def signin():
    win1 = Toplevel()
    l1 = Label(win1, text="注冊")
    l1.pack()  # 這里的side可以賦值為LEFT  RTGHT TOP  BOTTOM
    l2 = Label(win1, text="姓名:")
    l2.pack()  # 這里的side可以賦值為LEFT  RTGHT TOP  BOTTOM
    sheet_text1 = StringVar()
    sheet1 = Entry(win1, textvariable=sheet_text1)
    sheet1.pack()

    l3 = Label(win1, text="密碼:")
    l3.pack()  # 這里的side可以賦值為LEFT  RTGHT TOP  BOTTOM
    sheet_text2 = StringVar()
    sheet2 = Entry(win1, textvariable=sheet_text2)
    sheet2.pack()

    def on_click1():
        name = sheet_text1.get()
        pwd = sheet_text2.get()
        #調用處理新用戶窗口
        newuser(str(name),str(pwd))

    Button(win1, text="press", command=on_click1).pack()

#處理登錄窗口
def login():
    # win1 = Tk.winfo_toplevel(root)
    #焦點綁定到當前窗口,否則無法獲取輸入
    win1 = Toplevel()
    l4 = Label(win1, text="登錄")
    l4.pack()  # 這里的side可以賦值為LEFT  RTGHT TOP  BOTTOM
    l5 = Label(win1, text="姓名:")
    l5.pack()  # 這里的side可以賦值為LEFT  RTGHT TOP  BOTTOM
    sheet_text3 = StringVar()
    sheet3 = Entry(win1, textvariable=sheet_text3)
    sheet3.pack()

    l6 = Label(win1, text="密碼:")
    l6.pack()  # 這里的side可以賦值為LEFT  RTGHT TOP  BOTTOM
    sheet_text4 = StringVar()
    sheet4 = Entry(win1, textvariable=sheet_text4)
    sheet4.pack()

    def on_click2():
        name = sheet_text3.get()
        pwd = sheet_text4.get()
        olduser(name,pwd)

    Button(win1, text="press", command=on_click2).pack()

#退出程序
def quit1():
    root.quit()


if __name__ == '__main__':

    try:
        db = pymysql.connect("39.106.152.189", "study", "stfk0615", "study", use_unicode=True, charset='utf8')
        cursor = db.cursor()
    except:
        print("connect error!")

    root = Tk()
    root.title('用戶登錄窗口')

    #分別進入不同的窗口
    Button(root, text="注冊", command=signin).pack()
    Button(root, text="登錄", command=login).pack()
    Button(root, text="退出", command=quit1).pack()

    root.mainloop()

mysql_connect.py //封裝MySQL操作

import pymysql
# import traceback
def name_exist(cursor,name):
    exist_sql="""
    SELECT PASSWORD
    FROM INFO
    WHERE NAME='%s' ;"""%(str(name)) #字符串匹配替換 name
    cursor.execute(exist_sql)
    result=cursor.fetchall()
    if result:
        return True #表示存在、
    else:return False #表示不存在
def insert_data(db,cursor,name,password):
    #插入數據
    insert_sql="""
    INSERT INTO INFO(NAME,PASSWORD) VALUES('%s','%s');"""%(str(name),str(password))
    try:
        cursor.execute(insert_sql) #執行sql
        db.commit()
        return True
    except Exception:
        print(Exception.args)
        print('insert error')
        return False

def creat_table(cursor,name):
   # 創建數據表
    sql="""
        CREATE TABLE %s(
        NAME CHAR(20) NOT NULL,
        PASSWORD CHAR(20));
    """%(str(name))

    try:
        cursor.execute(sql)
        return True
    except:
        print("creat_table error")
        return False

def get_password(cursor,name):
    #得到name對應的密碼
    sql="""
    SELECT PASSWORD
    FROM INFO
    WHERE NAME='%s' ;"""%(str(name)) #字符串匹配替換 name
    try:
        cursor.execute(sql)
        result=cursor.fetchall() #得到所有的結果
        for a in result:  #遍歷結果 實際上返回的應該就只要一個值
            print(a[0])
            return a[0]
    except:
        print("get_error")
        return False

def connect():
    try:
        db = pymysql.connect("39.106.152.189", "study", "stfk0615","study"
                             ,use_unicode=True,charset='utf8')
        #db.set_charset('utf-8')
        cursor = db.cursor()
        # cursor.execute('SET NAMES utf8;')
        # cursor.execute('SET CHARACTER SET utf8;')
        # cursor.execute('SET character_set_connection=utf8;')
        return cursor
    except:
        print("connect error!")
        return False
if __name__=='__main__':
    db=pymysql.connect("39.106.152.189","study","stfk0615","study")
    cursor=db.cursor()

    db.close()

轉發請注明出處,謝謝。

本源碼用於 基於B/S三層體系結構,實現用戶身份驗證,實驗。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM