python實現(xiàn)登錄與注冊功能
本文實例為大家分享了python實現(xiàn)登錄與注冊的具體代碼,供大家參考,具體內(nèi)容如下
1. 案例介紹
本例設(shè)計一個用戶登錄和注冊模塊,使用 Tkinter 框架構(gòu)建界面,主要用到畫布、文本框、按鈕等組件。涉及知識點:Python Tkinter 界面編程、pickle 數(shù)據(jù)存儲。本例實現(xiàn)了基本的用戶登錄和注冊互動界面,并提供用戶信息存儲和驗證。pickle 是 python 語言的一個標準模塊,安裝 python 后已包含 pickle 庫,不需要單獨再安裝。pickle 模塊實現(xiàn)了基本的數(shù)據(jù)序列化和反序列化。通過 pickle 模塊的序列化操作能夠?qū)⒊绦蛑羞\行的對象信息保存到文件中去,永久存儲;通過 pickle 模塊的反序列化操作,能夠從文件中創(chuàng)建上一次程序保存的對象。本例難度為中級,適合具有 Python 基礎(chǔ)和 Tkinter 組件編程知識的用戶學習。
2. 示例效果


3. 示例源碼
import tkinter as tk
import pickle
import tkinter.messagebox
from PIL import Image, ImageTk
?
# 設(shè)置窗口---最開始的母體窗口
window = tk.Tk() ?# 建立一個窗口
window.title('歡迎登錄')
window.geometry('450x300') ?# 窗口大小為300x200
?
# 畫布
canvas = tk.Canvas(window, height=200, width=900)
# 加載圖片
im = Image.open("images/01.png")
image_file = ImageTk.PhotoImage(im)
# image_file = tk.PhotoImage(file='images/01.gif')
image = canvas.create_image(100, 40, anchor='nw', image=image_file)
canvas.pack(side='top')
?
# 兩個文字標簽,用戶名和密碼兩個部分
tk.Label(window, text='用戶名').place(x=100, y=150)
tk.Label(window, text='密 ?碼').place(x=100, y=190)
?
var_usr_name = tk.StringVar() ?# 講文本框的內(nèi)容,定義為字符串類型
var_usr_name.set('amoxiang@163.com') ?# 設(shè)置默認值
var_usr_pwd = tk.StringVar()
?
# 第一個輸入框-用來輸入用戶名的。
# textvariable 獲取文本框的內(nèi)容
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
# 第二個輸入框-用來輸入密碼的。
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)
?
?
def usr_login():
? ? usr_name = var_usr_name.get()
? ? usr_pwd = var_usr_pwd.get()
? ? try:
? ? ? ? with open('usrs_info.pickle', 'rb') as usr_file:
? ? ? ? ? ? usrs_info = pickle.load(usr_file)
? ? except FileNotFoundError:
? ? ? ? with open('usrs_info.pickle', 'wb') as usr_file:
? ? ? ? ? ? usrs_info = {'admin': 'admin'}
? ? ? ? ? ? pickle.dump(usrs_info, usr_file)
?
? ? if usr_name in usrs_info:
? ? ? ? if usr_pwd == usrs_info[usr_name]:
? ? ? ? ? ? tk.messagebox.showinfo(
? ? ? ? ? ? ? ? title='歡迎光臨', message=usr_name + ':請進入個人首頁,查看最新資訊')
? ? ? ? else:
? ? ? ? ? ? tk.messagebox.showinfo(message='錯誤提示:密碼不對,請重試')
? ? else:
? ? ? ? is_sign_up = tk.messagebox.askyesno('提示', '你還沒有注冊,請先注冊')
? ? ? ? print(is_sign_up)
? ? ? ? if is_sign_up:
? ? ? ? ? ? usr_sign_up()
?
?
# 注冊按鈕
def usr_sign_up():
? ? def sign_to_Mofan_Python():
? ? ? ? np = new_pwd.get()
? ? ? ? npf = new_pwd_confirm.get()
? ? ? ? nn = new_name.get()
? ? ? ? # 上面是獲取數(shù)據(jù),下面是查看一下是否重復注冊過
? ? ? ? with open('usrs_info.pickle', 'rb') as usr_file:
? ? ? ? ? ? exist_usr_info = pickle.load(usr_file)
? ? ? ? ? ? if np != npf:
? ? ? ? ? ? ? ? tk.messagebox.showerror('錯誤提示', '密碼和確認密碼必須一樣')
? ? ? ? ? ? elif nn in exist_usr_info:
? ? ? ? ? ? ? ? tk.messagebox.showerror('錯誤提示', '用戶名早就注冊了!')
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? exist_usr_info[nn] = np
? ? ? ? ? ? ? ? with open('usrs_info.pickle', 'wb') as usr_file:
? ? ? ? ? ? ? ? ? ? pickle.dump(exist_usr_info, usr_file)
? ? ? ? ? ? ? ? tk.messagebox.showinfo('歡迎', '你已經(jīng)成功注冊了')
? ? ? ? ? ? ? ? window_sign_up.destroy()
?
? ? # 點擊注冊之后,會彈出這個窗口界面。
? ? window_sign_up = tk.Toplevel(window)
? ? window_sign_up.title('歡迎注冊')
? ? window_sign_up.geometry('360x200') ?# 中間是x,而不是*號
?
? ? # 用戶名框--這里輸入用戶名框。
? ? new_name = tk.StringVar()
? ? new_name.set('amoxiang@163.com') ?# 設(shè)置的是默認值
? ? tk.Label(window_sign_up, text='用戶名').place(x=10, y=10)
? ? entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
? ? entry_new_name.place(x=100, y=10)
?
? ? # 新密碼框--這里輸入注冊時候的密碼
? ? new_pwd = tk.StringVar()
? ? tk.Label(window_sign_up, text='密 ?碼').place(x=10, y=50)
? ? entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
? ? entry_usr_pwd.place(x=100, y=50)
?
? ? # 密碼確認框
? ? new_pwd_confirm = tk.StringVar()
? ? tk.Label(window_sign_up, text='確認密碼').place(x=10, y=90)
? ? entry_usr_pwd_confirm = tk.Entry(
? ? ? ? window_sign_up, textvariable=new_pwd_confirm, show='*')
? ? entry_usr_pwd_confirm.place(x=100, y=90)
?
? ? btn_confirm_sign_up = tk.Button(
? ? ? ? window_sign_up, text=' 注 ?冊 ', command=sign_to_Mofan_Python)
? ? btn_confirm_sign_up.place(x=120, y=130)
?
?
# 創(chuàng)建注冊和登錄按鈕
btn_login = tk.Button(window, text=' 登 ?錄 ', command=usr_login)
btn_login.place(x=150, y=230) ?# 用place來處理按鈕的位置信息。
btn_sign_up = tk.Button(window, text=' 注 ?冊 ', command=usr_sign_up)
btn_sign_up.place(x=250, y=230)
?
window.mainloop()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch 調(diào)整某一維度數(shù)據(jù)順序的方法
今天小編就為大家分享一篇pytorch 調(diào)整某一維度數(shù)據(jù)順序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Pandas Series如何轉(zhuǎn)換為DataFrame
這篇文章主要介紹了Pandas Series如何轉(zhuǎn)換為DataFrame問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
詳解Python+Turtle繪制奧運標志的實現(xiàn)
turtle庫是Python標準庫之一,是入門級的圖形繪制函數(shù)庫。本文就將利用turtle庫繪制一個奧運標志—奧運五環(huán),感興趣的可以學習一下2022-02-02
pycharm創(chuàng)建一個python包方法圖解
在本篇文章中小編給大家分享了關(guān)于pycharm怎么創(chuàng)建一個python包的相關(guān)知識點,需要的朋友們學習下。2019-04-04

