快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

python做一个登录注册界面的方法

这篇文章主要介绍python做一个登录注册界面的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联服务项目包括绿园网站建设、绿园网站制作、绿园网页制作以及绿园网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,绿园网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到绿园省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

python做一个登录注册界面的方法:首先初始化一个window界面,并使用画布实现欢迎的logo;然后用代码实现登录和注册按钮;接着并进行登录判断代码;最后完成注册界面即可。

python做一个登录注册界面的方法

python做一个登录注册界面的方法:

一、登录界面

1、首先初始化一个window界面

        window = tk.Tk()
        window.title('Welcome to Mofan Python')
        window.geometry('450x300')
        .
        .
        .
        window.mainloop()

2、界面需要一个欢迎的logo,主要使用画布实现

        #welcome image
        #创建一个200X500的画布
        canvas =  tk.Canvas(window,height = 200,width = 500)
        #logo的路径
        image_file = tk.PhotoImage(file = 'E:\\welcome.gif')
        #什么位置插入logo图片
        image = canvas.create_image(0,0,anchor = 'nw',image = image_file)
        canvas.pack(side = 'top')

3、接下来主要是登录界面的代码实现

        tk.Label(window,text = 'Username:').place(x = 50,y = 150)
        tk.Label(window,text = 'Password:').place(x = 50,y = 190)
    
        var_usr_name = tk.StringVar()
        #默认值为MrZhangxd@python.com
        var_usr_name.set('MrZhangxd@python.com')
        var_usr_pwd = tk.StringVar()
        entry_usr_name = tk.Entry(window,textvariable = var_usr_name)
        entry_usr_name.place(x = 160,y = 150)
    
        entry_usr_pwd = tk.Entry(window,textvariable = var_usr_pwd,show ='*')
        entry_usr_pwd.place(x = 160,y = 190)

4、登录和注册按钮的实现代码

        #Login and Sign up button
        # command = usr_login 调用usr_login函数
        btn_login = tk.Button(window,text = 'Login',command = usr_login)
        btn_login.place(x = 170,y = 230)
        btn_sign_up = tk.Button(window,text = 'Sign up',command = usr_sign_up)
        btn_sign_up.place(x = 270,y = 230)

5、进行登录判断代码:主要用函数判断

声明usr_login函数

            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','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]:
                        tk.messagebox.showinfo(title = 'Welcome',message = 'How are you?' + usr_name)
                    else:
                        tk.messagebox.showinfo(message = 'Error,your password is wrong,try again.')
                else:
                    is_sign_up = tk.messagebox.askyesno('Welcome','You hava not sign up yet.Sign up today?')
            
                    if is_sign_up:
                        usr_sign_up()

登录用户不存在需要注册

二、注册界面

差不多和登录界面一样,然后不进行细细的说明了,有不懂地方的可以给下文邮件地址发邮箱。

    def usr_sign_up():
        def sign_to_Mofan_Python():
    
            np = new_pwd.get()
    
            npf = new_pwd_confirm.get()
    
            nn = new_name.get()
            with open('usrs_info','rb') as usr_file:
                exist_usr_info = pickle.load(usr_file)
            if np!= npf:
                tk.messagebox.showerror('Error','Password and confirm password must be the same!')
            elif nn in exist_usr_info:
                tk.messagebox.showerror('Error','The user has already signed up!')
            else:
                exist_usr_info[nn] = np
                with open('usrs_info.pickle','wb') as usr_file:
                    pickle.dump(exist_usr_info,usr_file)
                tk.messagebox.showinfo('Welcome','You have successfully signed up!')
                window_sign_up.destroy()
        window_sign_up = tk.Toplevel(window)
        window_sign_up.geometry('350x200')
        window_sign_up.title('Sign up window')
    
        new_name = tk.StringVar()
        new_name.set('MrZhangxd@python.com')
        tk.Label(window_sign_up,text = 'Username:').place(x = 10,y = 10)
        entry_new_name = tk.Entry(window_sign_up,textvariable = new_name)
        entry_new_name.place(x = 150,y = 10)
    
        new_pwd = tk.StringVar()
        tk.Label(window_sign_up,text = 'Password:').place(x = 10,y = 50)
        entry_new_pwd = tk.Entry(window_sign_up,textvariable = new_pwd,show = '*')
        entry_new_pwd.place(x = 150,y = 50)
    
        new_pwd_confirm = tk.StringVar()
        tk.Label(window_sign_up,text = 'Confirm password:').place(x = 10,y = 90)
        entry_comfirm_sign_up = tk.Entry(window_sign_up,textvariable = new_pwd_confirm,show = '*')
        entry_comfirm_sign_up.place(x = 150,y = 90)
    
        btn_comfirm_sign_up = tk.Button(window_sign_up,text = 'Sign up',command = sign_to_Mofan_Python)
        btn_comfirm_sign_up.place(x = 150,y = 130)

三、运行界面截图

登录界面

python做一个登录注册界面的方法

注册界面

python做一个登录注册界面的方法

以上是python做一个登录注册界面的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网页标题:python做一个登录注册界面的方法
文章出自:http://6mz.cn/article/jgggdc.html

其他资讯