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




總結
在這個項目中,我們使用Python的Tkinter庫和pymysql庫實現了一個簡單的用戶登錄和注冊系統。在這個項目中,我們使用到了以下幾個方面的知識和技能:
使用Tkinter庫創(chuàng)建圖形用戶界面(GUI):我們使用了Tkinter庫創(chuàng)建窗口、標簽、文本框和按鈕等控件,以及如何將它們布局在界面上。
與MySQL數據庫進行連接和操作:我們使用了pymysql庫連接到MySQL數據庫,執(zhí)行SQL查詢和插入操作,并且將數據庫中的數據與用戶輸入進行比對。
用戶登錄和注冊邏輯的實現:我們實現了用戶登錄和注冊的邏輯,包括驗證用戶輸入的賬號和密碼是否正確,以及在注冊時將新的賬號信息插入到數據庫中。
界面之間的切換和交互:我們在Tkinter中創(chuàng)建多個窗口,并且實現了從登錄界面到注冊界面的切換,以及注冊成功后返回到登錄界面的邏輯。
通過這個項目,我們掌握了基本的GUI設計和數據庫操作技能,以及用戶登錄和注冊系統的實現方法。這些知識和技能可以進一步應用到更復雜的項目中,例如開發(fā)更完整的用戶管理系統、圖像檢索系統等。同時,我們也了解到了Tkinter和pymysql庫的基本用法,為以后的項目開發(fā)打下了基礎。
到此這篇關于Python+Mysql實現登錄注冊的文章就介紹到這了,更多相關Python Mysql實現登錄注冊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python中IndexError與KeyError的解決方案和預防措施
在Python編程中,索引錯誤(IndexError)和鍵錯誤(KeyError)是兩種極為常見的異常類型,本文將深入剖析這兩種錯誤的本質、產生原因,并提供一系列實用的解決方案和預防措施,需要的朋友可以參考下2025-12-12

