python3.7使用tkinter和pickle写一个简单的注册登陆界面

这个代码比较简单,也是参考了别人写的代码,然后添加到IDE上进行运行。作了简单的修改,写到博客里,以便后续学习使用。

#_*_coding:utf-8 _*_
#auther    :LEO.TAN
#buildtime :2019/1/15 18:03
#file      :login.py
#IDE       :PyCharm

import pickle  # 存放数据的模块
import tkinter as tk
import tkinter.messagebox

window = tk.Tk()
window.title("注册登陆页面")
window.geometry("500x400")

canvas = tk.Canvas(window, height=300, width=500)
image_file = tk.PhotoImage(file="welcome.gif")
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')

tk.Label(window, text='User name:').place(x=50, y=200)
tk.Label(window, text='Password:').place(x=50, y=250)

var_usr_name = tk.StringVar()
var_usr_name.set('请输入用户名')

var_usr_pwd = tk.StringVar()
#var_usr_pwd.set('请输入密码')

entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=200)
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=250)


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: #注意这个地方用到了pickle可以百度一下使用方法
            usrs_info = pickle.load(usr_file)
    except FileNotFoundError:
        with open("usrs_info.pickle", "wb") as usr_file:  # with open with语句可以自动关闭资源
            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.showerror(message="Error,your password is wrong,try again")
    else:
        is_sign_up = tk.messagebox.askyesno("Welcome", "You have not sign up yet.Sign up today?")
        if is_sign_up:
            usr_sign_up()


def usr_sign_up():
    def sign_to_Python():
        signpwd = sign_pwd.get()
        signpwdconfirm = sign_pwd_confirm.get()
        signname = sign_name.get()
        with open("usrs_info.pickle", "rb") as usr_file:
            exist_usr_info = pickle.load(usr_file)
        if signpwd != signpwdconfirm:
            tk.messagebox.showerror("Error", "Password and confirm password must be the same!")
        elif signname in exist_usr_info:
            tk.messagebox.showerror("Error", "The user has already signed up! ")
        else:
            exist_usr_info[signname] = signpwd
            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!")
            # close window
            window_sign_up.destroy()

    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry("350x200")
    window_sign_up.title("注册页面")

    sign_name = tk.StringVar()
    sign_name.set('请输入用户名')
    tk.Label(window_sign_up, text="User name:").place(x=10, y=10)
    entry_new_name = tk.Entry(window_sign_up, textvariable=sign_name)
    entry_new_name.place(x=150, y=10)

    sign_pwd = tk.StringVar()
    tk.Label(window_sign_up, text="Password:").place(x=10, y=50)
    entry_usr_pwd = tk.Entry(window_sign_up, textvariable=sign_pwd, show='*')
    entry_usr_pwd.place(x=150, y=50)

    sign_pwd_confirm = tk.StringVar()
    tk.Label(window_sign_up, text="Confirm password:").place(x=10, y=90)
    entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=sign_pwd_confirm, show='*')
    entry_usr_pwd_confirm.place(x=150, y=90)

    btn_confirm_sign_up = tk.Button(window_sign_up, text="Sign up", command=sign_to_Python)
    btn_confirm_sign_up.place(x=150, y=130)


# login and sign up
btn_login = tk.Button(window, text="Login", command=usr_login)
btn_login.place(x=155, y=300)

btn_sign_up = tk.Button(window, text="Sign up", command=usr_sign_up)
btn_sign_up.place(x=270, y=300)

window.mainloop()

 

项目中使用到了一个gif图片,可以随便到网上下载一个,也可以使用我上传的图片(我上传的源码还在审核中,后续把链接发出来)。https://download.csdn.net/download/tfs080640234/10918973

运行如截图:

python3.7使用tkinter和pickle写一个简单的注册登陆界面_第1张图片

可以输入用户名,如果未注册,则会提示:

python3.7使用tkinter和pickle写一个简单的注册登陆界面_第2张图片

点击是或者点击sign up,会进到注册界面:

python3.7使用tkinter和pickle写一个简单的注册登陆界面_第3张图片

输入用户名和密码,确认密码后,会弹出注册成功:

python3.7使用tkinter和pickle写一个简单的注册登陆界面_第4张图片

然后再到登陆界面就可以输入刚才注册的用户名和密码,点击login

python3.7使用tkinter和pickle写一个简单的注册登陆界面_第5张图片

Ok,到此为止,一个简单的注册和登陆页面就实现了,使用到的知识点:

1.tkinter相关的知识,摆放标签和控件,canvas画图,

2.密码框使用*显示

3.保存用户名和密码使用kw模式的pickle.

    用于序列化的两个模块
     pickle: 用于python特有的类型和python的数据类型间进行转换
     pickle提供四个功能:dumps,dump,loads,load

    pickle可以存储什么类型的数据呢?

                   所有python支持的原生类型:布尔值,整数,浮点数,复数,字符串,字节,None。

                 由任何原生类型组成的列表,元组,字典和集合。

                 函数,类,类的实例

 

 

你可能感兴趣的:(python)