TK_GUI简单登录界面
import tkinter as tk
from tkinter.messagebox import *
import pickle
import os
root=tk.Tk()
root.title('登录')
root.geometry('500x400+500+100')
root.resizable(0,0)
#canvas=tk.Canvas(root,bg='blue',height=500,width=300)
imagepath=os.path.dirname(__file__) + './welcom.png'
image_file=tk.PhotoImage(file=imagepath)
#image=canvas.create_image(0,0,anchor='nw',image=image_file)
image_label=tk.Label(root,image=image_file)
image_label.place(x=0,y=0)
user_label=tk.Label(root,text='用户名:',font=('宋体',14,'bold'))
user_label.place(x=100,y=250)
usr_vars=tk.StringVar()
usr_vars.set('[email protected]')
user_enter=tk.Entry(root,textvariable=usr_vars)
user_enter.place(x=180,y=250)
passwd_label=tk.Label(root,text='密码:',font=('宋体',14,'bold'))
passwd_label.place(x=100,y=300)
passwd_vars=tk.StringVar()
passwd_enter=tk.Entry(root,textvariable=passwd_vars,show='*')
passwd_enter.place(x=180,y=300)
def sign_in():
usr_text=usr_vars.get()
passwd_text=passwd_vars.get()
try:
with open('user_info.pickle','rb') as userfile:
user_info=pickle.load(userfile)
except FileNotFoundError:
with open('user_info.pickle','wb') as userfile:
user_info={'admin':'admin'}
pickle.dump(user_info,userfile)
if usr_text in user_info:
if passwd_text==user_info[usr_text]:
showinfo('Welcome','How are you?'+ usr_text)
else:
if usr_text=='' or passwd_text=='':
showinfo('错误','用户名或者密码不能为空')
else:
showinfo('Error','your password wrong,try again.')
else:
if usr_text=='' or passwd_text=='':
showinfo('错误','用户名或者密码不能为空')
else:
on_sign_up=tk.messagebox.askyesno(title='Welcome',message='You have not sign up yet.Sign up?')
if on_sign_up:
sign_up()
def sign_up():
def sign_to_python():
nn=enter1_vars.get()
np=enter2_vars.get()
npf=enter3_vars.get()
with open('user_info.pickle','rb') as userfile:
exit_user_info=pickle.load(userfile)
if np!=npf:
showinfo('Error','Password and confirm password')
elif nn in exit_user_info:
showinfo('Error','The user has already signed up!')
else:
exit_user_info[nn]=np
with open('user_info.pickle','wb') as userfile:
pickle.dump(exit_user_info,userfile)
showinfo('Welcom','You have sign up successfully')
window.destroy()
window=tk.Toplevel(root)
window.title('sign up')
window.geometry('300x200')
label1=tk.Label(window,text='注册账号:',font=('宋体',14,'bold'),width=12)
label1.place(x=0,y=10)
label2=tk.Label(window,text='新密码:',font=('宋体',14,'bold'),width=15)
label2.place(x=0,y=50)
label3=tk.Label(window,text='密码确认:',font=('宋体',14,'bold'),width=13)
label3.place(x=0,y=80)
enter1_vars=tk.StringVar()
enter1_vars.set('[email protected]')
enter1=tk.Entry(window,textvariable=enter1_vars)
enter1.place(x=120,y=10)
enter2_vars=tk.StringVar()
enter2=tk.Entry(window,textvariable=enter2_vars)
enter2.place(x=120,y=50)
enter3_vars=tk.StringVar()
enter3=tk.Entry(window,textvariable=enter3_vars)
enter3.place(x=120,y=80)
bt1=tk.Button(window,text='确定',font=('宋体',14,'bold'),command=sign_to_python)
bt1.place(x=100,y=150)
bt2=tk.Button(window,text='退出',font=('宋体',14,'bold'),command=window.quit)
bt2.place(x=180,y=150)
sign_in_bt=tk.Button(root,text='Login',font=('宋体',14,'bold'),command=sign_in)
sign_in_bt.place(x=230,y=350)
sign_up_bt=tk.Button(root,text='sign up',font=('宋体',14,'bold'),command=sign_up)
sign_up_bt.place(x=350,y=350)
root.mainloop()