深入了解python的tkinter實(shí)現(xiàn)簡單登錄
通過python的tkinter實(shí)現(xiàn)簡單的注冊登錄
參考文章:http://www.dbjr.com.cn/article/197751.htm
編寫一個(gè)用戶登錄界面,用戶可以登錄賬戶信息,如果賬戶已經(jīng)存在,可以直接登錄,登錄名或者登錄密碼輸入錯(cuò)誤會(huì)提示,如果賬戶不存在,提示用戶注冊,點(diǎn)擊注冊進(jìn)去注冊頁面,輸入注冊信息,確定后便可以返回登錄界面進(jìn)行登錄。進(jìn)入個(gè)人主頁可以對個(gè)人信息進(jìn)行修改。
代碼
import tkinter as tk from tkinter import messagebox # import this to fix messagebox error import pickle username = '' # 登錄界面 def welcome(): 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) usr_file.close() except FileNotFoundError: with open('usrs_info.pickle', 'wb') as usr_file: usrs_info = {'admin': 'admin'} pickle.dump(usrs_info, usr_file) usr_file.close() if usr_name in usrs_info: if usr_pwd == usrs_info[usr_name]: tk.messagebox.showinfo(title='Welcome', message='How are you? ' + usr_name) # 進(jìn)入主頁 global username username= usr_name print(username) window.destroy() index_page() else: tk.messagebox.showerror(message='Error, your password is wrong, try again.') else: is_sign_up = tk.messagebox.askyesno('Welcome', 'You have not signed up yet. Sign up today?') if is_sign_up: usr_sign_up() window = tk.Tk() window.title('Welcome to the login page!') window.geometry('450x300') # welcome image canvas = tk.Canvas(window, height=200, width=500) image_file = tk.PhotoImage(file='welcome.gif') image = canvas.create_image(0, 0, anchor='nw', image=image_file) canvas.pack(side='top') # user information tk.Label(window, text='User name: ').place(x=50, y=150) tk.Label(window, text='Password: ').place(x=50, y=190) var_usr_name = tk.StringVar() entry_usr_name = tk.Entry(window, textvariable=var_usr_name) entry_usr_name.place(x=160, y=150) var_usr_pwd = tk.StringVar() entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*') entry_usr_pwd.place(x=160, y=190) # login and sign up button btn_login = tk.Button(window, text='Login', command=usr_login) btn_login.place(x=170, y=230) btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up) btn_sign_up.place(x=270, y=230) window.mainloop() # 個(gè)人信息頁面 def index_page(): global username index_window = tk.Tk() index_window.title('請選擇你要進(jìn)行的操作') index_window.geometry('300x200') tk.Label(index_window,text="你好!"+username).place(x=50,y=50) # 修改個(gè)人信息 def change_personal_info(): # 新窗口 change_info__window = tk.Toplevel() change_info__window.title('修改個(gè)人信息') change_info__window.geometry('400x300') # 修改個(gè)人信息點(diǎn)擊頁面 def change_info_click(): name = new_name.get() psw = new_password.get() conpsw = new_password_confirm.get() with open('usrs_info.pickle', 'rb') as f: usrs_info = pickle.load(f) f.close() if conpsw!= psw: tk.messagebox.showerror(title='Error', message='兩次密碼不一致請重新輸入!') else: if name in usrs_info: tk.messagebox.showerror(title='Error', message='用戶名已存在,請重新?lián)Q一個(gè)新用戶名') else: # 創(chuàng)建新 usrs_info.pop(username) usrs_info[name] = psw with open('usrs_info.pickle', 'wb') as file: pickle.dump(usrs_info, file) f.close() tk.messagebox.showinfo(title='修改成功', message='修改成功!') change_info__window.destroy() tk.messagebox.showinfo(title='請重新登錄!', message='請重新登錄!') index_window.destroy() welcome() # 修改用戶名變量 new_name = tk.StringVar() tk.Label(change_info__window, text="用戶名:").place(x=50, y=50) new_name_entry = tk.Entry(change_info__window, show=None, textvariable=new_name).place(x=120, y=50) # 修改新密碼變量 new_password = tk.StringVar() tk.Label(change_info__window, text='密碼:').place(x=50, y=80) new_password_entry = tk.Entry(change_info__window, show="*", textvariable=new_password).place(x=120, y=80) # 修改新確認(rèn)密碼變量 new_password_confirm = tk.StringVar() tk.Label(change_info__window, text="確認(rèn)密碼:").place(x=50, y=110) new_password_confirm_entry = tk.Entry(change_info__window, show="*", textvariable=new_password_confirm).place(x=120, y=110) # 修改信息按鈕綁定 sign_window_button = tk.Button(change_info__window, text='確認(rèn)', command=change_info_click).place(x=150, y=140) # 修改個(gè)人信息按鈕綁定 change_info_button = tk.Button(index_window,text='修改個(gè)人信息',command=change_personal_info).place(x=100,y=100) # 注冊頁面 def usr_sign_up(): sign_up_window = tk.Toplevel() sign_up_window.title('注冊') sign_up_window.geometry('400x200') # 注冊點(diǎn)擊事件 def sign_up_hit(): name = new_name.get() psw = new_password.get() conpsw = new_password_confirm.get() with open('usrs_info.pickle', 'rb') as usr_file: usrs_info = pickle.load(usr_file) if psw!=conpsw: tk.messagebox.showerror(title='Error', message='兩次密碼不一致請重新輸入!') else: if name in usrs_info: tk.messagebox.showerror(title='Error', message='用戶名已存在') else: # 創(chuàng)建新 usrs_info[name] = psw with open('usrs_info.pickle', 'wb') as f: pickle.dump(usrs_info,f) tk.messagebox.showinfo(title='注冊成功', message='注冊成功!') sign_up_window.destroy() # 注冊名變量 new_name = tk.StringVar() tk.Label(sign_up_window,text="注冊名:").place(x=50,y=50) new_name_entry = tk.Entry(sign_up_window,show=None,textvariable=new_name).place(x=120,y=50) # 注冊密碼變量 new_password = tk.StringVar() tk.Label(sign_up_window,text='密碼:').place(x=50,y=80) new_password_entry = tk.Entry(sign_up_window,show="*",textvariable=new_password).place(x=120,y=80) # 注冊確認(rèn)密碼變量 new_password_confirm= tk.StringVar() tk.Label(sign_up_window,text="確認(rèn)密碼:").place(x=50,y=110) new_password_confirm_entry = tk.Entry(sign_up_window,show="*",textvariable=new_password_confirm).place(x=120,y=110) # 注冊按鈕和點(diǎn)擊事件綁定 sign_window_button = tk.Button(sign_up_window,text='注冊',command=sign_up_hit).place(x=150,y=140) welcome()
截圖
登錄頁面
注冊頁面
個(gè)人主頁
修改個(gè)人信息失敗
修改個(gè)人信息成功
重新登錄twb
發(fā)現(xiàn)已注銷除非重新注冊。
發(fā)現(xiàn)成功修改個(gè)人信息。登陸上。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
超詳細(xì)注釋之OpenCV操作圖像平移轉(zhuǎn)換
這篇文章主要介紹了OpenCV操作圖像平移轉(zhuǎn)換,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Python?matplotlib實(shí)戰(zhàn)之箱型圖繪制
箱型圖(Box?Plot),也稱為盒須圖或盒式圖,是一種用作顯示一組數(shù)據(jù)分布情況的統(tǒng)計(jì)圖,因型狀如箱子而得名,本文主要為大家介紹了如何使用Matplotlib繪制箱型圖,需要的小伙伴可以參考下2023-08-08python實(shí)現(xiàn)websocket的客戶端壓力測試
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)websocket的客戶端壓力測試,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06PyTorch學(xué)習(xí)之軟件準(zhǔn)備與基本操作總結(jié)
這篇文章主要介紹了PyTorch學(xué)習(xí)之軟件準(zhǔn)備與基本操作總結(jié),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05python目標(biāo)檢測數(shù)據(jù)增強(qiáng)的代碼參數(shù)解讀及應(yīng)用
這篇文章主要為大家介紹了python目標(biāo)檢測數(shù)據(jù)增強(qiáng)的代碼參數(shù)解讀及應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05用Python制作檢測Linux運(yùn)行信息的工具的教程
這篇文章主要介紹了用Python制作檢測Linux運(yùn)行信息的工具的教程,主要是用CPython讀取運(yùn)行系統(tǒng)的硬件參數(shù)、網(wǎng)絡(luò)傳輸流量統(tǒng)計(jì)等,需要的朋友可以參考下2015-04-04Python中搜索和替換文件中的文本的實(shí)現(xiàn)(四種)
本文主要介紹了Python中搜索和替換文件中的文本的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10