import sys
import tkinter.messagebox
from tkinter import *
import tkinter as tk
import json
window = tk.Tk()
window.geometry('500x500+500+250')
window.title('this is title !')
def user_log_in():
user_name = uname.get()
user_pwd = password.get()
try:
with open('usr_info.json','r') as usr_file:
users_info = json.load(usr_file)
except FileNotFoundError :
with open('usr_info.json','w') as usr_file :
users_info = {'admin':'admin'}
json.dump(users_info,usr_file)
if user_name in users_info :
if user_pwd == users_info[user_name] :
tk.messagebox.showinfo('登录成功 ','欢迎您:'+user_name)
else:
tk.messagebox.showerror('错误','密码错误')
elif user_name == '' or user_pwd == '' :
tk.messagebox.showerror('错误','用户名或密码为空')
else :
flag = tk.messagebox.askyesno('欢迎','您好!您还没有注册,是否注册')
if flag :
rgister()
canvas = tk.Canvas(window,bg='purple',height=300,width=500)
img_file = tk.PhotoImage(file='pic.gif')
img=canvas.create_image(250,150,anchor='center',image=img_file)
canvas.place(x=0,y=0,anchor='nw')
login=tk.Button(window,text='login',width=10,command=user_log_in)
login.place(y=416,x=240)
rg_name=tk.StringVar
rg_psd=tk.StringVar
rg_repsd=tk.StringVar
def user_register():
sign_name = rg_name.get()
sign_psw = rg_psd.get()
pwd_confirm = rg_repsd.get()
try:
with open('usr_info.json', 'r') as usr_file:
exist_usr_info = json.load(usr_file)
print(exist_usr_info)
except FileNotFoundError:
exist_usr_info = {}
if sign_name in exist_usr_info:
tk.messagebox.showerror('错误', '用户名已存在')
elif sign_psw == '' or sign_name == '':
tk.messagebox.showerror('错误', '用户名或密码为空')
elif sign_psw != pwd_confirm:
tk.messagebox.showerror('错误', '密码前后不一致')
else:
exist_usr_info[sign_name] = sign_psw
with open('usr_info.json', 'w') as usr_file:
json.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
def rgister():
global rg_name
global rg_repsd
global rg_psd
rg=tk.Toplevel(window)
rg.title('注册')
rg.geometry('300x300+550+350')
Label(rg, text='用户名:').place(x=50,y=20,anchor='nw')
rg_name=tk.Entry(rg);rg_name.place(x=110,y=20,anchor='nw')
Label(rg, text='密码:').place(x=50,y=50,anchor='nw')
rg_psd = tk.Entry(rg,show='*');rg_psd.place(x=110,y=50,anchor='nw')
Label(rg, text='确认密码:').place(x=50,y=75,anchor='nw')
rg_repsd = tk.Entry(rg, show='*');rg_repsd.place(x=110,y=80,anchor='nw')
tk.Button(rg, text='确认注册',command=user_register).place(x=100, y=110, anchor='nw')
rg.mainloop()
regiset=tk.Button(window,text='register',width=10,command=rgister)
regiset.place(y=416,x=140)
tk.Button(window,text='退出',command=sys.exit).place(y=416,x=360)
Label(window,text='账号:').place(y=350,x=140)
uname=tk.Entry(window)
uname.place(y=350,x=175)
Label(window,text='密码:').place(y=385,x=140)
password=tk.Entry(window,show='*')
password.place(y=385,x=175)
window.mainloop()
|