python tkinter制作用戶登錄界面的簡單實(shí)現(xiàn)
本文只是幾年前學(xué)習(xí)的tkinter的時(shí)候?qū)懙臏y試程序,十分之簡陋,只是學(xué)習(xí)用,沒什么其他用處。
學(xué)習(xí)一下莫煩Python的tkinter教程,根據(jù)教程制作了用戶登錄注冊頁?;竟δ転闄z查登錄、注冊。
運(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')
#畫布放置圖片
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)簽 用戶名密碼
tk.Label(window,text='用戶名:').place(x=100,y=150)
tk.Label(window,text='密碼:').place(x=100,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)
#登錄函數(shù)
def usr_log_in():
#輸入框獲取用戶名密碼
usr_name=var_usr_name.get()
usr_pwd=var_usr_pwd.get()
#從本地字典獲取用戶信息,如果沒有則新建本地?cái)?shù)據(jù)庫
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)
#判斷用戶名和密碼是否匹配
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ò)誤')
#用戶名密碼不能為空
elif usr_name=='' or usr_pwd=='' :
tk.messagebox.showerror(message='用戶名或密碼為空')
#不在數(shù)據(jù)庫中彈出是否注冊的框
else:
is_signup=tk.messagebox.askyesno('歡迎','您還沒有注冊,是否現(xiàn)在注冊')
if is_signup:
usr_sign_up()
#注冊函數(shù)
def usr_sign_up():
#確認(rèn)注冊時(shí)的相應(yīng)函數(shù)
def signtowcg():
#獲取輸入框內(nèi)的內(nèi)容
nn=new_name.get()
np=new_pwd.get()
npf=new_pwd_confirm.get()
#本地加載已有用戶信息,如果沒有則已有用戶信息為空
try:
with open('usr_info.pickle','rb') as usr_file:
exist_usr_info=pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info={}
#檢查用戶名存在、密碼為空、密碼前后不一致
if nn in exist_usr_info:
tk.messagebox.showerror('錯(cuò)誤','用戶名已存在')
elif np =='' or nn=='':
tk.messagebox.showerror('錯(cuò)誤','用戶名或密碼為空')
elif np !=npf:
tk.messagebox.showerror('錯(cuò)誤','密碼前后不一致')
#注冊信息沒有問題則將用戶名密碼寫入數(shù)據(jù)庫
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('歡迎','注冊成功')
#注冊成功關(guān)閉注冊框
window_sign_up.destroy()
#新建注冊界面
window_sign_up=tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注冊')
#用戶名變量及標(biāo)簽、輸入框
new_name=tk.StringVar()
tk.Label(window_sign_up,text='用戶名:').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='請輸入密碼:').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='請?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)注冊按鈕及位置
bt_confirm_sign_up=tk.Button(window_sign_up,text='確認(rèn)注冊',
command=signtowcg)
bt_confirm_sign_up.place(x=150,y=130)
#退出的函數(shù)
def usr_sign_quit():
window.destroy()
#登錄 注冊按鈕
bt_login=tk.Button(window,text='登錄',command=usr_log_in)
bt_login.place(x=140,y=230)
bt_logup=tk.Button(window,text='注冊',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制作用戶登錄界面的簡單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python tkinter用戶登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch框架實(shí)現(xiàn)mnist手寫庫識別(與tensorflow對比)
這篇文章主要介紹了Pytorch框架實(shí)現(xiàn)mnist手寫庫識別(與tensorflow對比),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
對變量賦值的理解--Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11
python在多玩圖片上下載妹子圖的實(shí)現(xiàn)代碼
學(xué)python的第二天,想寫個(gè)東西出來玩玩,于是就寫了這個(gè),供那些才學(xué)一天的參考參考也行2013-08-08
Python3實(shí)現(xiàn)mysql連接和數(shù)據(jù)框的形成(實(shí)例代碼)
這篇文章主要介紹了Python3實(shí)現(xiàn)mysql連接和數(shù)據(jù)框的形成,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作示例
這篇文章主要介紹了Python使用numpy產(chǎn)生正態(tài)分布隨機(jī)數(shù)的向量或矩陣操作,簡單描述了正態(tài)分布的概念并結(jié)合實(shí)例形式分析了Python使用numpy模塊結(jié)合matplotlib繪制正態(tài)分布曲線圖相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
Python報(bào)錯(cuò)ValueError: cannot reindex from
當(dāng)處理Pandas數(shù)據(jù)框(DataFrame)時(shí),你是否遇到過ValueError: cannot reindex from a duplicate axis的報(bào)錯(cuò)?這個(gè)問題通常發(fā)生在嘗試對DataFrame進(jìn)行重索引時(shí),如果索引有重復(fù)值,就會觸發(fā)這個(gè)錯(cuò)誤,下面,我們將探討這個(gè)問題并提供解決方法2024-09-09
Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹實(shí)現(xiàn)方法示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之字典樹實(shí)現(xiàn)方法,可實(shí)現(xiàn)針對單詞出現(xiàn)次數(shù)的統(tǒng)計(jì)功能,涉及Python樹結(jié)構(gòu)的定義、遍歷及統(tǒng)計(jì)等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
python使用collections模塊的容器數(shù)據(jù)類型高效處理數(shù)據(jù)
這篇文章主要為大家介紹了python使用collections模塊的容器數(shù)據(jù)類型高效處理數(shù)據(jù)的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

