Python+Mysql實(shí)現(xiàn)登錄注冊(cè)完整代碼示例
簡(jiǎn)介
基于Tkinter的Python程序,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的用戶登錄和注冊(cè)系統(tǒng)。程序連接了一個(gè)MySQL數(shù)據(jù)庫(kù),用戶可以通過(guò)輸入正確的用戶名和密碼進(jìn)行登錄,或者注冊(cè)新的用戶賬號(hào)。
代碼中創(chuàng)建了兩個(gè)窗口,一個(gè)用于登錄,一個(gè)用于注冊(cè)。用戶可以在登錄窗口輸入用戶名和密碼,點(diǎn)擊登錄按鈕進(jìn)行登錄操作。如果用戶沒(méi)有賬號(hào),可以點(diǎn)擊注冊(cè)按鈕打開注冊(cè)窗口,輸入新的用戶名和密碼,然后點(diǎn)擊注冊(cè)按鈕進(jìn)行注冊(cè)操作。
注冊(cè)信息會(huì)存儲(chǔ)在MySQL數(shù)據(jù)庫(kù)中,當(dāng)用戶注冊(cè)成功后,會(huì)收到提示并返回到登錄窗口進(jìn)行登錄操作。如果用戶輸入的賬號(hào)或密碼錯(cuò)誤,會(huì)收到相應(yīng)的錯(cuò)誤提示。
整個(gè)程序的界面使用了Tkinter庫(kù)進(jìn)行設(shè)計(jì),包括標(biāo)簽、文本框和按鈕等控件。數(shù)據(jù)庫(kù)連接和操作使用了pymysql庫(kù)。
總體來(lái)說(shuō),這個(gè)程序是一個(gè)簡(jiǎn)單的用戶登錄和注冊(cè)系統(tǒng),可以幫助用戶管理賬號(hào)信息,并且實(shí)現(xiàn)了與數(shù)據(jù)庫(kù)的交互。
工作環(huán)境
pycharm 2023.3.12
MySql 8.0.22
所用三方包
- tkinter()
- PyMysql(1.1.0)
代碼實(shí)現(xiàn)
界面布局
登錄界面
# 登錄頁(yè)面控件
lable1 = tkinter.Label(root, text="賬號(hào):", font=('Arial', 15), width=10) # 創(chuàng)建賬號(hào)標(biāo)簽
lable1.place(x=80, y=10) # 設(shè)置賬號(hào)標(biāo)簽的位置
text1 = tkinter.Text(root, width=10, height=1, bg="white", fg="black", font=("宋體", 18), bd='0') # 創(chuàng)建賬號(hào)輸入文本框
text1.place(x=170, y=10) # 設(shè)置賬號(hào)輸入文本框的位置
lable2 = tkinter.Label(root, text="密碼:", font=('Arial', 15), width=10) # 創(chuàng)建密碼標(biāo)簽
lable2.place(x=80, y=80) # 設(shè)置密碼標(biāo)簽的位置
text2 = tkinter.Entry(root, show="*", width=10, bg="white", fg="black", font=("宋體", 18), bd='0') # 創(chuàng)建密碼輸入文本框
text2.place(x=170, y=80) # 設(shè)置密碼輸入文本框的位置
button0 = tkinter.Button(root, text="登錄", command=open_login_window) # 創(chuàng)建登錄按鈕,并將按鈕與登錄函數(shù)關(guān)聯(lián)
button0.place(x=180, y=150) # 設(shè)置登錄按鈕位置
button1 = tkinter.Button(root, text="退出", command=root.quit) # 創(chuàng)建退出按鈕,并設(shè)置點(diǎn)擊事件為退出程序
button1.place(x=250, y=150) # 設(shè)置退出按鈕位置
# 注冊(cè)按鈕
register_button = tkinter.Button(root, text="注冊(cè)", command=open_register_window)
register_button.place(x=110, y=150)注冊(cè)界面
# 注冊(cè)頁(yè)面控件
register_window = tkinter.Toplevel(root)
register_window.title("注冊(cè)")
register_window.geometry("400x250")
register_window.protocol("WM_DELETE_WINDOW", close_register_window) # 隱藏窗口而不是關(guān)閉
register_window.withdraw() # 隱藏窗口
register_lable1 = tkinter.Label(register_window, text="賬號(hào):", font=('Arial', 15), width=10)
register_lable1.place(x=80, y=10)
register_text1 = tkinter.Text(register_window, width=10, height=1, bg="white", fg="black", font=("宋體", 18), bd='0')
register_text1.place(x=170, y=10)
register_lable2 = tkinter.Label(register_window, text="密碼:", font=('Arial', 15), width=10)
register_lable2.place(x=80, y=80)
register_text2 = tkinter.Entry(register_window, show="*", width=10, bg="white", fg="black", font=("宋體", 18), bd='0')
register_text2.place(x=170, y=80)
register_button = tkinter.Button(register_window, text="注冊(cè)", command=register)
register_button.place(x=180, y=150)登錄功能實(shí)現(xiàn)
def open_login_window():
# 創(chuàng)建游標(biāo)對(duì)象
cursor = connection.cursor()
sql = """SELECT username, password FROM admin""" # sql查詢語(yǔ)句,查找admin表中username和password兩個(gè)值
cursor.execute(sql) # 執(zhí)行查詢語(yǔ)句
# 返回?cái)?shù)據(jù)庫(kù)查詢的所有信息
results = cursor.fetchall()
# print(results)
# 關(guān)閉游標(biāo)
cursor.close()
username = text1.get("1.0", tkinter.END).strip() # 從文本框中獲取輸入的賬戶信息
password = text2.get().strip() # 從文本框中獲取輸入的密碼信息
login_success = False
for temp in results:
if username == temp[0] and password == temp[1]:
login_success = True
break
if login_success:
messagebox.showinfo('成功', '登錄成功')
root.quit()
else:
messagebox.showinfo('Error', '賬號(hào)或密碼錯(cuò)誤,請(qǐng)重試')注冊(cè)功能實(shí)現(xiàn)
def register():
username = register_text1.get("1.0", tkinter.END).strip()
password = register_text2.get().strip()
if username and password:
cursor = connection.cursor()
sql = "INSERT INTO admin (username, password) VALUES (%s, %s)"
cursor.execute(sql, (username, password))
connection.commit()
cursor.close()
messagebox.showinfo('成功', '注冊(cè)成功,請(qǐng)登錄')
register_window.withdraw()
root.deiconify()
else:
messagebox.showinfo('Error', '賬號(hào)或密碼不能為空')數(shù)據(jù)庫(kù)連接
# 建立數(shù)據(jù)庫(kù)連接
connection = pymysql.connect(
host='localhost', # 數(shù)據(jù)庫(kù)主機(jī)名,一般為localhost
port=3306, # 數(shù)據(jù)庫(kù)端口號(hào),默認(rèn)為3306
user='root', # 你的數(shù)據(jù)庫(kù)用戶名,一般為root
passwd='database_password', # 你的數(shù)據(jù)庫(kù)密碼
db='database_name', # 數(shù)據(jù)庫(kù)名稱
charset='utf8' # 字符編碼
)注冊(cè)頁(yè)面控制函數(shù)
def open_register_window():
register_window.deiconify()
root.withdraw()
def close_register_window():
register_window.withdraw()
root.deiconify()程序運(yùn)行效果




總結(jié)
在這個(gè)項(xiàng)目中,我們使用Python的Tkinter庫(kù)和pymysql庫(kù)實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的用戶登錄和注冊(cè)系統(tǒng)。在這個(gè)項(xiàng)目中,我們使用到了以下幾個(gè)方面的知識(shí)和技能:
使用Tkinter庫(kù)創(chuàng)建圖形用戶界面(GUI):我們使用了Tkinter庫(kù)創(chuàng)建窗口、標(biāo)簽、文本框和按鈕等控件,以及如何將它們布局在界面上。
與MySQL數(shù)據(jù)庫(kù)進(jìn)行連接和操作:我們使用了pymysql庫(kù)連接到MySQL數(shù)據(jù)庫(kù),執(zhí)行SQL查詢和插入操作,并且將數(shù)據(jù)庫(kù)中的數(shù)據(jù)與用戶輸入進(jìn)行比對(duì)。
用戶登錄和注冊(cè)邏輯的實(shí)現(xiàn):我們實(shí)現(xiàn)了用戶登錄和注冊(cè)的邏輯,包括驗(yàn)證用戶輸入的賬號(hào)和密碼是否正確,以及在注冊(cè)時(shí)將新的賬號(hào)信息插入到數(shù)據(jù)庫(kù)中。
界面之間的切換和交互:我們?cè)赥kinter中創(chuàng)建多個(gè)窗口,并且實(shí)現(xiàn)了從登錄界面到注冊(cè)界面的切換,以及注冊(cè)成功后返回到登錄界面的邏輯。
通過(guò)這個(gè)項(xiàng)目,我們掌握了基本的GUI設(shè)計(jì)和數(shù)據(jù)庫(kù)操作技能,以及用戶登錄和注冊(cè)系統(tǒng)的實(shí)現(xiàn)方法。這些知識(shí)和技能可以進(jìn)一步應(yīng)用到更復(fù)雜的項(xiàng)目中,例如開發(fā)更完整的用戶管理系統(tǒng)、圖像檢索系統(tǒng)等。同時(shí),我們也了解到了Tkinter和pymysql庫(kù)的基本用法,為以后的項(xiàng)目開發(fā)打下了基礎(chǔ)。
到此這篇關(guān)于Python+Mysql實(shí)現(xiàn)登錄注冊(cè)的文章就介紹到這了,更多相關(guān)Python Mysql實(shí)現(xiàn)登錄注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中IndexError與KeyError的解決方案和預(yù)防措施
在Python編程中,索引錯(cuò)誤(IndexError)和鍵錯(cuò)誤(KeyError)是兩種極為常見的異常類型,本文將深入剖析這兩種錯(cuò)誤的本質(zhì)、產(chǎn)生原因,并提供一系列實(shí)用的解決方案和預(yù)防措施,需要的朋友可以參考下2025-12-12
利用Python進(jìn)行金融數(shù)據(jù)分析的全過(guò)程
金融數(shù)據(jù)分析在現(xiàn)代金融行業(yè)中扮演著至關(guān)重要的角色,通過(guò)使用Python編程語(yǔ)言,我們可以對(duì)大量金融數(shù)據(jù)進(jìn)行處理、分析和可視化,從而獲得有價(jià)值的洞察,本篇文章將介紹如何使用Python進(jìn)行金融數(shù)據(jù)分析,需要的朋友可以參考下2024-08-08
python3 selenium自動(dòng)化 下拉框定位的例子
今天小編就為大家分享一篇python3 selenium自動(dòng)化 下拉框定位的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python?f-string實(shí)現(xiàn)高效字符串格式化
f-string,稱為格式化字符串常量(formatted?string?literals),是Python3.6新引入的一種字符串格式化方法,本篇主要講解f-string常見的使用方法,希望對(duì)大家有所幫助2025-05-05

