1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| import pickle import tkinter as tk import tkinter.messagebox
window = tk.Tk()
window.title('登 录')
window.geometry('500x400')
l1 = tk.Label(window,text='用户名:',font= 12).place(x=100,y=82) l2 = tk.Label(window,text='密 码:',font= 12).place(x=100,y=152)
var_user_name = tk.StringVar() entry_user_name = tk.Entry(window,textvariable=var_user_name,font= 18,width=30,bd =5) entry_user_name.place(x=180,y=80)
var_user_password = tk.StringVar() entry_user_password = tk.Entry(window,textvariable=var_user_password,show='*',font= 18,width=30,bd =5) entry_user_password.place(x=180,y=150)
def user_login_in(): user_name = var_user_name.get() user_password = var_user_password.get() try: with open('user_info.pickle','rb') as user_file: user_info = pickle.load(user_file) except FileNotFoundError: with open('user_info.pickle','wb') as user_file: user_info = {'admin':'admin'} pickle.dump(user_info,user_file) if user_name ==' ' or user_password == ' ': tk.messagebox.showerror(message='用户名或密码不能为空!') elif user_name in user_info: if user_password == user_info[user_name]: tk.messagebox.showinfo(title='welcome',message='欢迎您:'+user_name) else: tk.messagebox.showerror(message='密码错误!') else: is_signup = tk.messagebox.askyesno('欢迎','您还没有注册,是否现在注册') if is_signup: user_sign_up() def user_sign_up(): def registration(): input_name = new_name.get() input_password = new_password.get() input_password_confirm =new_password_confirm.get() try: with open('user_info.pickle','rb') as user_file: exist_user_info = pickle.load((user_file)) except FileNotFoundError: exist_user_info = {} if input_name in exist_user_info: tk.messagebox.showerror('错误','用户名已存在!') elif input_password =='' or input_name =='': tk.messagebox.showerror('错误','用户名或密码不能为空!') elif input_password != input_password_confirm: tk.messagebox.showerror('错误','密码前后不一致!') else: exist_user_info[input_name] = input_password with open('user_info.pickle','wb') as user_file: pickle.dump(exist_user_info,user_file) tk.messagebox.showinfo('欢迎','注册成功!') window_sign_up.destroy() window_sign_up = tk.Toplevel(window) window_sign_up.geometry('350x200') window_sign_up.title('注册')
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)
new_password = tk.StringVar() tk.Label(window_sign_up,text='请输入密码:').place(x=10,y=50) tk.Entry(window_sign_up,textvariable=new_password,show='*').place(x=150,y=50)
new_password_confirm = tk.StringVar() tk.Label(window_sign_up,text='请再次确认密码:').place(x=10,y=90) tk.Entry(window_sign_up,textvariable=new_password_confirm,show='*').place(x=150,y=90)
bt_confirm_sign_up = tk.Button(window_sign_up,text='确认注册',command=registration) bt_confirm_sign_up.place(x=150,y=130) def user_sign_out(): window.destroy() bt_login = tk.Button(window,text='登 录',width=30,font= 18,bg='DarkTurquoise',activebackground='Turquoise',command=user_login_in) bt_login.place(x=150,y=210)
bt_register = tk.Button(window,text='注册',width=25,command= user_sign_up) bt_register.place(x=150,y=270)
bt_exit = tk.Button(window,text='退出',width=5,command = user_sign_out) bt_exit.place(x=350,y=270) window.mainloop()
|