Python登陆系统

前言

#源码见文末公众号哈#

Python登陆系统

登录系统 

一个简单的登录系统包含了登录账户、注册账户、修改密码以及注销账户的操作。

1. 登录账户

登录系统主要需要判断账户是否存在,不存在就注册一个账户,如果第一次登录系统,我们需要先新建一个文件,保存管理员的账户以及密码,具体代码如下:

def user_login():
    user_name=name.get()
    user_key=key.get()
    try:
        with open('d:\\user.pickle','rb') as user_file:
            user_info=pickle.load(user_file)
    except FileNotFoundError:
        with open('d:\\user.pickle','wb') as user_file:
            user_info={'admin':'12345'}
            pickle.dump(user_info,user_file)
            user_file.close()
    if user_name in user_info:
        if user_key == user_info[user_name]:
            tkinter.messagebox.showinfo(title='提示', message='登录成功!' )
        else:
            tkinter.messagebox.showerror('提示','密码错误!请重新输入')
    else:  
        exist = tkinter.messagebox.askyesno('提示','该用户名未注册,是否立即注册?')
        if exist:
            user_sign()

2. 注册账户 

注册账户时需要判断该账户是否存在,如果已经存在时提醒用户重新注册即可。

注册时还要输入两次密码,判断是否一致。 

具体代码如下:

def user_sign():
    sign_root=tk.Toplevel(root)
    sign_root.title('注册账户')
    screenheight=sign_root.winfo_screenheight()
    screenwidth=sign_root.winfo_screenwidth()
    h=220
    w=300
    x=(screenwidth-w)//2
    y=(screenheight-h)//2
    sign_root.geometry("%dx%d+%d+%d"%(w,h,x,y))
    new_name=tk.StringVar()
    tk.Label(sign_root,text='欢迎来到注册系统',font=('宋体',20),bg='white',fg='red',width=20,height=1).place(x=7,y=10)
    tk.Label(sign_root,text='新的账户',font=('宋体',12)).place(x=20,y=60)
    entry_name=tk.Entry(sign_root,textvariable=new_name,font=('宋体',12),show=None).place(x=100,y=60)
    new_key=tk.StringVar()
    tk.Label(sign_root,text='新的密码',font=('宋体',12)).place(x=20,y=100)
    entry_key=tk.Entry(sign_root,textvariable=new_key,font=('宋体',12),show='*').place(x=100,y=100)
    same_key=tk.StringVar()
    tk.Label(sign_root,text='请确认密码',font=('宋体',12)).place(x=10,y=140)
    entry_keys=tk.Entry(sign_root,textvariable=same_key,font=('宋体',12),show='*').place(x=100,y=140)
    def sign_check():
        name=new_name.get()
        key1=new_key.get()
        key2=same_key.get()
        with open('d:\\user.pickle','rb') as user_file:
            user_info=pickle.load(user_file)
            user_file.close()
        if name in user_info:
            tkinter.messagebox.showerror('提示','该账户已存在!')
        elif key1!=key2:
            tkinter.messagebox.showerror('提示','两次输入密码不一致,请重新输入!')
        else:
            user_info[name]=key1
            with open(r'd:\\user.pickle','wb') as user_file:
                pickle.dump(user_info,user_file)
                user_file.close()
            tkinter.messagebox.showinfo('提示','注册成功!')
            sign_root.destroy()
    tk.Button(sign_root,text='注册',font=('宋体',12),bg='red',fg='white',width=5,height=1,command=sign_check).place(x=130,y=180)    

3. 修改密码

修改密码时需要先确定需要修改密码的账户是否存在,不存在无法修改密码。

修改密码时如果账户存在,还要判断原密码是否一致以及新输入的两次密码是否一致。

具体源码如下:

def user_change():
    change_root=tk.Toplevel(root)
    old_name=tk.StringVar()
    old_key=tk.StringVar()
    new_key=tk.StringVar()
    same_key=tk.StringVar()
    change_root.title('修改密码')
    screenheight=change_root.winfo_screenheight()
    screenwidth=change_root.winfo_screenwidth()
    h=220
    w=300
    x=(screenwidth-w)//2
    y=(screenheight-h)//2
    change_root.geometry("%dx%d+%d+%d"%(w,h,x,y))
    tk.Label(change_root,text='欢迎来到修改密码系统',font=('宋体',20),bg='white',fg='green',width=20,height=1).place(x=5,y=10)
    tk.Label(change_root,text='请输入你的账号',font=('宋体',12),width=15,height=1).place(x=5,y=60)
    tk.Entry(change_root,textvariable=old_name,show=None).place(x=130,y=60)
    tk.Label(change_root,text='请输入原始密码',font=('宋体',12),width=15,height=1).place(x=5,y=90)
    tk.Entry(change_root,textvariable=old_key,show=None).place(x=130,y=90)
    tk.Label(change_root,text='请输入修改密码',font=('宋体',12),width=15,height=1).place(x=5,y=120)
    tk.Entry(change_root,textvariable=new_key,show='*').place(x=130,y=120)
    tk.Label(change_root,text='请确认你的密码',font=('宋体',12),width=15,height=1).place(x=5,y=150)
    tk.Entry(change_root,textvariable=same_key,show='*').place(x=130,y=150)
    def change_check():
        name=old_name.get()
        key1=old_key.get()
        key2=new_key.get()
        key3=same_key.get()
        with open("d:\\user.pickle",'rb') as user_file:
            user_info=pickle.load(user_file)
            user_file.close()
        if name in user_info:
            if key1==user_info[name]:
                if key2==key3:
                    user_info[name]=key2
                    with open('d:\\user.pickle','wb') as user_file:
                        pickle.dump(user_info,user_file)
                        user_file.close()
                    tkinter.messagebox.showinfo('提示',"修改成功!")
                    change_root.destroy()
                else:
                    tkinter.messagebox.showerror('提示','两次密码不一致,请重新输入!')
            else:
                tkinter.messagebox.showerror('提示','密码错误,请重新输入!')
        else:
            exist=tkinter.messagebox.askyesno('提示','该账户不存在,是否立即注册账户?')
            if exist:
                user_sign()      
    tk.Button(change_root,text='修改',font=('宋体',12),bg='green',fg='white',command=change_check).place(x=130,y=180) 

4. 注销账户

注销账户时需要先判断账户是否存在,然后输入两次原密码,当输入的两次原密码相同时即可注销

成功。

具体代码如下:

def user_del():
    old_name=tk.StringVar()
    old_key=tk.StringVar()
    same_key=tk.StringVar()
    del_root=tk.Toplevel(root)
    del_root.title('注销账户')
    screenheight=del_root.winfo_screenheight()
    screenwidth=del_root.winfo_screenwidth()
    h=220
    w=300
    x=(screenwidth-w)//2
    y=(screenheight-h)//2
    del_root.geometry("%dx%d+%d+%d"%(w,h,x,y))
    tk.Label(del_root,text='欢迎来到注销账户系统',font=('宋体',20),bg='white',fg='black',width=20,height=1).place(x=5,y=10)
    tk.Label(del_root,text='请输入你的账号',font=('宋体',12),width=15,height=1).place(x=5,y=60)
    tk.Entry(del_root,textvariable=old_name,show=None).place(x=130,y=60)
    tk.Label(del_root,text='请输入原始密码',font=('宋体',12),width=15,height=1).place(x=5,y=100)
    tk.Entry(del_root,textvariable=old_key,show=None).place(x=130,y=100)
    tk.Label(del_root,text='请确认你的密码',font=('宋体',12),width=15,height=1).place(x=5,y=140)
    tk.Entry(del_root,textvariable=same_key,show='*').place(x=130,y=140)    
    def del_check():
        name=old_name.get()
        key1=old_key.get()
        key2=same_key.get()
        with open('d:\\user.pickle','rb') as user_file:
            user_info=pickle.load(user_file)
            user_file.close()
        if name in user_info:
            if key1==key2:
                if key1==user_info[name]:
                    user_info.pop(name)
                    with open('d:\\user.pickle','wb') as user_file:
                        pickle.dump(user_info,user_file)
                        user_file.close()
                    tkinter.messagebox.showinfo('提示','注销成功!')
                else:
                    tkinter.messagebox.showerror('提示','密码错误!')
            else:
                tkinter.messagebox.showerror('提示','两次密码不一致!')
        else:
            exist=tkinter.messagebox.askyesno('提示','该账户不存在,是否立即注册?')
            if exist:
                user_sign()
    tk.Button(del_root,text='注销',font=('宋体',12),bg='black',fg='white',command=del_check).place(x=130,y=180)  

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年3月2日 下午10:31
下一篇 2023年3月2日 下午10:32

相关推荐