本文只是幾年前學(xué)習(xí)的tkinter的時(shí)候?qū)懙臏y(cè)試程序,十分之簡(jiǎn)陋,只是學(xué)習(xí)用,沒(méi)什么其他用處。
# -*- 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()
#從本地字典獲取用戶信息,如果沒(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)
#判斷用戶名和密碼是否匹配
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ù)庫(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()
#本地加載已有用戶信息,如果沒(méi)有則已有用戶信息為空
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ò)誤','密碼前后不一致')
#注冊(cè)信息沒(méi)有問(wèn)題則將用戶名密碼寫入數(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è)')
#用戶名變量及標(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='請(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制作用戶登錄界面的簡(jiǎn)單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python tkinter用戶登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!