溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄

發(fā)布時(shí)間:2022-01-11 11:17:49 來源:億速云 閱讀:238 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄”,在日常操作中,相信很多人在Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄

Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄

Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄

項(xiàng)目結(jié)構(gòu):

Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄

源代碼:

# -*- coding: utf-8 -*-
"""
@date:  2022/01/09 17:40
@author: Anker
@python:v3.10
"""
 
import tkinter as tk
import tkinter.messagebox
import pymysql
 
# 定義要執(zhí)行的創(chuàng)建表的SQL語句
test_sql = """
                CREATE TABLE IF NOT EXISTS user(
                id INT auto_increment PRIMARY KEY,
                name varchar(20) not null,
                password varchar(20) not null
                )ENGINE=innodb DEFAULT CHARSET=utf8;
           """
 
# 登錄窗口
window = tk.Tk()
window.title('學(xué)生考試系統(tǒng)')
window.geometry('800x500')
 
# 登錄背景圖片
canvas = tk.Canvas(window, height=1920, width=1080)
login_background = tk.PhotoImage(file='./view.png')
login_image = canvas.create_image(0, 0, anchor='nw', image=login_background)
canvas.pack(side='top')
 
# 用戶名密碼標(biāo)簽
tk.Label(window, text='用戶名:', bg='yellow').place(x=300, y=200)
tk.Label(window, text='密   碼:', bg='yellow').place(x=300, y=250)
 
# 用戶名輸入框
var_user_name = tk.StringVar()
entry_user_name = tk.Entry(window, textvariable=var_user_name)
entry_user_name.place(x=370, y=200)
 
# 密碼輸入框
var_user_pwd = tk.StringVar()
entry_user_pwd = tk.Entry(window, textvariable=var_user_pwd, show='*')
entry_user_pwd.place(x=370, y=250)
 
 
# 登錄函數(shù)
def user_login():
    # 輸入框獲取用戶名密碼
    user_name = var_user_name.get()
    user_password = var_user_pwd.get()
    # 連接test_sql數(shù)據(jù)庫
    conn = pymysql.connect(host="localhost", user="root", password="123456", database="test_sql", charset="utf8")
    curs = conn.cursor()
    # 執(zhí)行SQL語句,創(chuàng)建user數(shù)據(jù)表
    curs.execute(test_sql)
    # 執(zhí)行SQL語句,從user數(shù)據(jù)表中查詢name和password字段值
    curs.execute("SELECT name,password FROM user")
    # 將數(shù)據(jù)庫查詢的結(jié)果保存在result中
    result = curs.fetchall()
    # fetchone()函數(shù)它的返回值是單個(gè)的元組, 也就是一行記錄, 如果沒有結(jié)果, 那就會(huì)返回null
    # fetchall()函數(shù)它的返回值是多個(gè)元組, 即返回多個(gè)行記錄, 如果沒有結(jié)果, 返回的是()
    # assert result, "數(shù)據(jù)庫無該用戶信息"   # 添加斷言,判斷數(shù)據(jù)庫有無該用戶信息,沒有就直接斷言錯(cuò)誤
 
    # 登錄賬號(hào)操作
    name_list = [it[0] for it in result]    # 從數(shù)據(jù)庫查詢的result中遍歷查詢?cè)M中第一個(gè)元素name
    # 判斷用戶名或密碼不能為空
    if not(user_name and user_password):
        tk.messagebox.showwarning(title='警告', message='用戶名或密碼不能為空')
    # 判斷用戶名和密碼是否匹配
    elif user_name in name_list:
        if user_password == result[name_list.index(user_name)][1]:
            tk.messagebox.showinfo(title='歡迎您', message='       登錄成功!\r\n當(dāng)前登錄賬號(hào)為:' + user_name)
            selection()
        else:
            tk.messagebox.showerror(title='錯(cuò)誤', message='密碼輸入錯(cuò)誤')
    # 賬號(hào)不在數(shù)據(jù)庫中,則彈出是否注冊(cè)的框
    else:
        is_signup = tk.messagebox.askyesno(title='提示', message='該賬號(hào)不存在,是否現(xiàn)在注冊(cè)?')
        if is_signup:
            user_register()
 
 
# 注冊(cè)函數(shù)
def user_register():
    # 確認(rèn)注冊(cè)函數(shù)
    def register_confirm():
        # 獲取輸入框內(nèi)的內(nèi)容
        name = new_name.get()
        password = new_password.get()
        password_confirm = new_password_confirm.get()
        # 先在本地手動(dòng)創(chuàng)建一個(gè)test_sql數(shù)據(jù)庫,然后連接該數(shù)據(jù)庫
        conn = pymysql.connect(host="localhost", user="root", password="123456", database="test_sql", charset="utf8")
        curs = conn.cursor()
 
        # 注冊(cè)賬號(hào)操作
        try:
            # 執(zhí)行SQL語句,創(chuàng)建user數(shù)據(jù)表
            curs.execute(test_sql)
            # 向user數(shù)據(jù)表中插入語句
            insert_sql = "INSERT INTO user(name, password) VALUES ('%s', '%s')" % (name, password)
            # 讀取user數(shù)據(jù)表中的name和password字段值
            read_sql = f'''select * from user where name = "{name}" and password = "{password}" '''
            user_data = curs.execute(read_sql)
            # 判斷注冊(cè)賬號(hào)和密碼
            if not (name and password):
                tk.messagebox.showwarning(title='警告', message='注冊(cè)賬號(hào)或密碼不能為空')
            elif password != password_confirm:
                tk.messagebox.showwarning(title='警告', message='兩次密碼輸入不一致,請(qǐng)重新輸入')
            else:
                if user_data.real:
                    tk.messagebox.showwarning(title='警告', message='該注冊(cè)賬號(hào)已存在')
                else:
                    curs.execute(insert_sql)
                    tk.messagebox.showinfo(title='恭喜您', message='      注冊(cè)成功!\r\n注冊(cè)賬號(hào)為:' + name)
                    print("數(shù)據(jù)插入成功")
            # 提交到數(shù)據(jù)庫執(zhí)行
            conn.commit()
            curs.close()
        except IOError:
            print("數(shù)據(jù)插入失敗")
            conn.rollback()
        # 關(guān)閉數(shù)據(jù)庫連接
        conn.close()
        window_sign_up.destroy()
 
    # 注冊(cè)窗口
    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('歡迎注冊(cè)')
 
    # 注冊(cè)賬號(hào)及標(biāo)簽、輸入框
    new_name = tk.StringVar()
    tk.Label(window_sign_up, bg='green', text='注冊(cè)賬號(hào):').place(x=50, y=10)
    tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
 
    # 注冊(cè)密碼及標(biāo)簽、輸入框
    new_password = tk.StringVar()
    tk.Label(window_sign_up, bg='green', text='密      碼:').place(x=50, y=50)
    tk.Entry(window_sign_up, textvariable=new_password, show='*').place(x=150, y=50)
 
    # 重復(fù)密碼及標(biāo)簽、輸入框
    new_password_confirm = tk.StringVar()
    tk.Label(window_sign_up, bg='green', text='確認(rèn)密碼:').place(x=50, y=90)
    tk.Entry(window_sign_up, textvariable=new_password_confirm, show='*').place(x=150, y=90)
 
    # 確認(rèn)注冊(cè)按鈕及位置
    bt_confirm_sign_up = tk.Button(window_sign_up, bg='green', text='確認(rèn)注冊(cè)', command=register_confirm)
    bt_confirm_sign_up.place(x=150, y=130)
 
 
# 選擇題函數(shù)
def selection():
 
    def wrong():
        tk.messagebox.showerror(title='錯(cuò)誤', message='抱歉,您答錯(cuò)了')
 
    def right():
        tk.messagebox.showinfo(title='提示', message='恭喜您,答對(duì)了')
 
    # 選擇題窗口
    window_options = tk.Toplevel(window)
    window_options.geometry('350x200')
    window_options.title('選擇題')
    # 在圖形界面上創(chuàng)建一個(gè)標(biāo)簽label用以顯示并放置
    var = tk.StringVar()  # 定義一個(gè)var用來將radiobutton的值和Label的值聯(lián)系在一起.
    lab = tk.Label(window_options, bg='red', fg='white', width=50)
    lab.pack()
    lab.config(text='第1題:兩個(gè)銳角均為60度的三角形是什么三角形()' + var.get())
    # 創(chuàng)建3個(gè)radiobutton選項(xiàng),其中variable=var, value='A'表示:當(dāng)鼠標(biāo)選中其中一個(gè)選項(xiàng),把value的值A(chǔ)放到變量var中,然后賦值給variable
    radio1 = tk.Radiobutton(window_options, text='A:銳角三角形', variable=var, value='A', command=wrong)
    radio1.pack()
    radio2 = tk.Radiobutton(window_options, text='B:鈍角三角形', variable=var, value='B', command=wrong)
    radio2.pack()
    radio3 = tk.Radiobutton(window_options, text='C:等邊三角形', variable=var, value='C', command=right)
    radio3.pack()
    radio4 = tk.Radiobutton(window_options, text='D:直角三角形', variable=var, value='D', command=wrong)
    radio4.pack()
 
 
# 注冊(cè)和登錄按鈕
bt_register = tk.Button(window, bg='yellow', text='注冊(cè)', command=user_register)
bt_register.place(x=380, y=300)
bt_login = tk.Button(window, bg='yellow', text='登錄', command=user_login)
bt_login.place(x=440, y=300)
 
# 主循環(huán)
window.mainloop()

到此,關(guān)于“Python+Tkinter連接本地MySQL數(shù)據(jù)庫怎么實(shí)現(xiàn)注冊(cè)登錄”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI