python tkinter制作用戶(hù)登錄界面的簡(jiǎn)單實(shí)現(xiàn)
本文只是幾年前學(xué)習(xí)的tkinter的時(shí)候?qū)懙臏y(cè)試程序,十分之簡(jiǎn)陋,只是學(xué)習(xí)用,沒(méi)什么其他用處。
學(xué)習(xí)一下莫煩Python的tkinter教程,根據(jù)教程制作了用戶(hù)登錄注冊(cè)頁(yè)?;竟δ転闄z查登錄、注冊(cè)。
運(yùn)行如下:





代碼如下:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 5 10:34:10 2018
@author: Administrator
"""
import tkinter as tk
import tkinter.messagebox
import pickle
#窗口
window=tk.Tk()
window.title('歡迎進(jìn)入學(xué)習(xí)系統(tǒng)')
window.geometry('450x300')
#畫(huà)布放置圖片
canvas=tk.Canvas(window,height=300,width=500)
imagefile=tk.PhotoImage(file='qm.png')
image=canvas.create_image(0,0,anchor='nw',image=imagefile)
canvas.pack(side='top')
#標(biāo)簽 用戶(hù)名密碼
tk.Label(window,text='用戶(hù)名:').place(x=100,y=150)
tk.Label(window,text='密碼:').place(x=100,y=190)
#用戶(hù)名輸入框
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)
#登錄函數(shù)
def usr_log_in():
#輸入框獲取用戶(hù)名密碼
usr_name=var_usr_name.get()
usr_pwd=var_usr_pwd.get()
#從本地字典獲取用戶(hù)信息,如果沒(méi)有則新建本地?cái)?shù)據(jù)庫(kù)
try:
with open('usr_info.pickle','rb') as usr_file:
usrs_info=pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle','wb') as usr_file:
usrs_info={'admin':'admin'}
pickle.dump(usrs_info,usr_file)
#判斷用戶(hù)名和密碼是否匹配
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='welcome',
message='歡迎您:'+usr_name)
else:
tk.messagebox.showerror(message='密碼錯(cuò)誤')
#用戶(hù)名密碼不能為空
elif usr_name=='' or usr_pwd=='' :
tk.messagebox.showerror(message='用戶(hù)名或密碼為空')
#不在數(shù)據(jù)庫(kù)中彈出是否注冊(cè)的框
else:
is_signup=tk.messagebox.askyesno('歡迎','您還沒(méi)有注冊(cè),是否現(xiàn)在注冊(cè)')
if is_signup:
usr_sign_up()
#注冊(cè)函數(shù)
def usr_sign_up():
#確認(rèn)注冊(cè)時(shí)的相應(yīng)函數(shù)
def signtowcg():
#獲取輸入框內(nèi)的內(nèi)容
nn=new_name.get()
np=new_pwd.get()
npf=new_pwd_confirm.get()
#本地加載已有用戶(hù)信息,如果沒(méi)有則已有用戶(hù)信息為空
try:
with open('usr_info.pickle','rb') as usr_file:
exist_usr_info=pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info={}
#檢查用戶(hù)名存在、密碼為空、密碼前后不一致
if nn in exist_usr_info:
tk.messagebox.showerror('錯(cuò)誤','用戶(hù)名已存在')
elif np =='' or nn=='':
tk.messagebox.showerror('錯(cuò)誤','用戶(hù)名或密碼為空')
elif np !=npf:
tk.messagebox.showerror('錯(cuò)誤','密碼前后不一致')
#注冊(cè)信息沒(méi)有問(wèn)題則將用戶(hù)名密碼寫(xiě)入數(shù)據(jù)庫(kù)
else:
exist_usr_info[nn]=np
with open('usr_info.pickle','wb') as usr_file:
pickle.dump(exist_usr_info,usr_file)
tk.messagebox.showinfo('歡迎','注冊(cè)成功')
#注冊(cè)成功關(guān)閉注冊(cè)框
window_sign_up.destroy()
#新建注冊(cè)界面
window_sign_up=tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注冊(cè)')
#用戶(hù)名變量及標(biāo)簽、輸入框
new_name=tk.StringVar()
tk.Label(window_sign_up,text='用戶(hù)名:').place(x=10,y=10)
tk.Entry(window_sign_up,textvariable=new_name).place(x=150,y=10)
#密碼變量及標(biāo)簽、輸入框
new_pwd=tk.StringVar()
tk.Label(window_sign_up,text='請(qǐng)輸入密碼:').place(x=10,y=50)
tk.Entry(window_sign_up,textvariable=new_pwd,show='*').place(x=150,y=50)
#重復(fù)密碼變量及標(biāo)簽、輸入框
new_pwd_confirm=tk.StringVar()
tk.Label(window_sign_up,text='請(qǐng)?jiān)俅屋斎朊艽a:').place(x=10,y=90)
tk.Entry(window_sign_up,textvariable=new_pwd_confirm,show='*').place(x=150,y=90)
#確認(rèn)注冊(cè)按鈕及位置
bt_confirm_sign_up=tk.Button(window_sign_up,text='確認(rèn)注冊(cè)',
command=signtowcg)
bt_confirm_sign_up.place(x=150,y=130)
#退出的函數(shù)
def usr_sign_quit():
window.destroy()
#登錄 注冊(cè)按鈕
bt_login=tk.Button(window,text='登錄',command=usr_log_in)
bt_login.place(x=140,y=230)
bt_logup=tk.Button(window,text='注冊(cè)',command=usr_sign_up)
bt_logup.place(x=210,y=230)
bt_logquit=tk.Button(window,text='退出',command=usr_sign_quit)
bt_logquit.place(x=280,y=230)
#主循環(huán)
window.mainloop()
到此這篇關(guān)于python tkinter制作用戶(hù)登錄界面的簡(jiǎn)單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python tkinter用戶(hù)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫(kù)簡(jiǎn)單實(shí)現(xiàn)注冊(cè)登錄
- 基于python的Tkinter編寫(xiě)登陸注冊(cè)界面
- 深入了解python的tkinter實(shí)現(xiàn)簡(jiǎn)單登錄
- Python中tkinter的用戶(hù)登錄管理的實(shí)現(xiàn)
- Python Tkinter 簡(jiǎn)單登錄界面的實(shí)現(xiàn)
- Python+tkinter模擬“記住我”自動(dòng)登錄實(shí)例代碼
- Python+Tkinter簡(jiǎn)單實(shí)現(xiàn)注冊(cè)登錄功能
相關(guān)文章
Pytorch框架實(shí)現(xiàn)mnist手寫(xiě)庫(kù)識(shí)別(與tensorflow對(duì)比)
這篇文章主要介紹了Pytorch框架實(shí)現(xiàn)mnist手寫(xiě)庫(kù)識(shí)別(與tensorflow對(duì)比),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
對(duì)變量賦值的理解--Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
python在多玩圖片上下載妹子圖的實(shí)現(xiàn)代碼
學(xué)python的第二天,想寫(xiě)個(gè)東西出來(lái)玩玩,于是就寫(xiě)了這個(gè),供那些才學(xué)一天的參考參考也行2013-08-08
Python3實(shí)現(xiàn)mysql連接和數(shù)據(jù)框的形成(實(shí)例代碼)
這篇文章主要介紹了Python3實(shí)現(xiàn)mysql連接和數(shù)據(jù)框的形成,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作示例
這篇文章主要介紹了Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作,簡(jiǎn)單描述了正態(tài)分布的概念并結(jié)合實(shí)例形式分析了Python使用numpy模塊結(jié)合matplotlib繪制正態(tài)分布曲線(xiàn)圖相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Python報(bào)錯(cuò)ValueError: cannot reindex from
當(dāng)處理Pandas數(shù)據(jù)框(DataFrame)時(shí),你是否遇到過(guò)ValueError: cannot reindex from a duplicate axis的報(bào)錯(cuò)?這個(gè)問(wèn)題通常發(fā)生在嘗試對(duì)DataFrame進(jìn)行重索引時(shí),如果索引有重復(fù)值,就會(huì)觸發(fā)這個(gè)錯(cuò)誤,下面,我們將探討這個(gè)問(wèn)題并提供解決方法2024-09-09
Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹(shù)實(shí)現(xiàn)方法示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹(shù)實(shí)現(xiàn)方法,可實(shí)現(xiàn)針對(duì)單詞出現(xiàn)次數(shù)的統(tǒng)計(jì)功能,涉及Python樹(shù)結(jié)構(gòu)的定義、遍歷及統(tǒng)計(jì)等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
python使用collections模塊的容器數(shù)據(jù)類(lèi)型高效處理數(shù)據(jù)
這篇文章主要為大家介紹了python使用collections模塊的容器數(shù)據(jù)類(lèi)型高效處理數(shù)據(jù)的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

