from tkinter import *
import pickle
from tkinter import messagebox
window = Tk()
window.title("登录系统")
window.geometry('450x300')
# 用画布加载图片
canvas = Canvas(window, height=200, width=500)
image_file = PhotoImage(file='welcome.gif')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')
# 标签
Label(window,text='用户名:').place(x=100, y=150)
Label(window,text='密 码:').place(x=100, y=190)
# 输入框
var_usr_name = StringVar()
var_usr_name.set('example@python.com')
var_usr_pwd = StringVar()
entry_usename = Entry(window, textvariable=var_usr_name)
entry_usename.place(x=150, y=150)
entry_password = Entry(window, textvariable=var_usr_pwd, show='*')
entry_password.place(x=150, y=190)
def usr_login():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usrs_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]:
messagebox.showinfo(title='Welcome', message='登录成功')
else:
messagebox.showerror(title='error', message='密码错误,请重新登录')
else:
is_sign_up = messagebox.askyesno(title='注册吧!', message='老哥,你还没有注册,请赶紧注册,然后在使用!!')
if is_sign_up:
usr_sign_up()
def usr_sign_up():
# 窗口上面的窗口
window_sign_up = Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册页面')
new_name = StringVar()#将输入的注册名赋值给变量
new_name.set('example@python.com')#将最初显示定为'example@python.com'
Label(window_sign_up, text='用户名: ').place(x=80, y= 10)#将`User name:`放置在坐标(10,10)。
entry_new_name = Entry(window_sign_up, textvariable=new_name)#创建一个注册名的`entry`,变量为`new_name`
entry_new_name.place(x=150, y=10)#`entry`放置在坐标(150,10).
new_pwd = StringVar()
Label(window_sign_up, text='密 码: ').place(x=80, y=50)
entry_usr_pwd = Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=150, y=50)
new_pwd_confirm = StringVar()
Label(window_sign_up, text='验 证: ').place(x=80, y= 90)
entry_usr_pwd_confirm = Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=150, y=90)
def sign_to_xl():
# 以下三行就是获取我们注册时所输入的信息
np = new_pwd.get()
npf = new_pwd_confirm.get()
nn = new_name.get()
# 这里是打开我们记录数据的文件,将注册信息读入
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
# 这里是判断,如果两次密码输入不准确,则提示错误
if np != npf:
messagebox.showerror(title='error', message='两次输入不一致,请重新输入!')
# 如果用户已经在我们的数据文件中,提示错误
elif nn in exist_usr_info:
messagebox.showerror(title='error', message='不能注册,这个账户已经存在!')
# 如果输入无以上错误,则将信息注册
else:
exist_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
messagebox.showinfo(title='Welcome', message='你已经成功登陆!!')
# 销毁窗口
window_sign_up.destory()
btn_comfirm_sign_up = Button(window_sign_up, text='注册', command=sign_to_xl)
btn_comfirm_sign_up.place(x=150, y=130)
btn_conceal = Button(window_sign_up, text='取消', command=window_sign_up.quit)
btn_conceal.place(x=230, y=130)
# 按钮
btn_login = Button(window, text='登录', command=usr_login)
btn_login.place(x=130, y=230)
btn_sign_up = Button(window, text='注册', command=usr_sign_up)
btn_sign_up.place(x=230, y=230)
mainloop()