tkinter:登陆窗口_账户创建和修改 .2021-01-12修

import tkinter as tk
import tkinter.messagebox
import pickle
# 导入功能模块

window = tk.Tk()
window.title('我的窗口')
window.geometry('480x640')
# 创建主窗口

canvas = tk.Canvas(window,
                   width=500, height=200,
                   )
# 创建画布组件

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=150)
tk.Label(window, text='Password:').place(x=50, y=190)
# 创建两个标签

var_usr_name = tk.StringVar()
# 创建一个字符串可变变量

var_usr_name.set('example@python.com')
# 为字符串可变变量赋值

entry_usr_name = tk.Entry(window,
                          textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
var_usr_pwd = tk.StringVar()
entry_var_usr_pwd = tk.Entry(window,
                             textvariable=var_usr_pwd,
                             show='*')
entry_var_usr_pwd.place(x=160, y=190)
# 创建两个输入栏


def usr_login():
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()
    # 获取两个输入框中的内容

    try:
        with open('user_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)
    # 尝试访问文件,如果不存在则新建目标文件
    # pickle.dump(obj, file, protocol=None,) 序列化封装usrs_info对象数据到usr_file文件中
    # 目前pickle.dump(obj, file, protocol=None,) 必须用二进制数据流读写.python3默认使用3协议

    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(title='Error', message='Your password is wrong,try again.')
    else:
        is_sign_up = tk.messagebox.askyesno(title='Welcome',
                                            message='You have not sign up yet.Sign up today?')
        if is_sign_up:
            usr_sign_up()
    # 根据输入框内的数据和文件数据的比较结果弹出相应的窗口,
    # 再根据用户的交互结果继续运行


def usr_sign_up():
    def sign_to():
        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:
            tk.messagebox.showerror(title='错误', message='两次密码输入不一致请重新输入')
        elif nn in exist_usr_info:
            tk.messagebox.showerror(title='错误', message='输入的用户名已存在请重新输入')
        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(title='欢迎', message='注册成功')
            window_sign_up.destroy()
    # 输入的注册信息写入文件

    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('注册窗口')
    # 在 window窗口上 创建 一个窗口

    new_name = tk.StringVar()
    new_name.set('example@python.com')
    tk.Label(window_sign_up, text='User name:').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_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd)
    entry_usr_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_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_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)
    btn_confirm_sign_up.place(x=150, y=130)
    # 按钮按下后写入注册信息


# 自定义函数

btn_login = tk.Button(window,
                      text='Login',
                      command=usr_login)
btn_login.place(x=100, y=230)
btn_sign_up = tk.Button(window,
                        text='Sign up',
                        command=usr_sign_up)
btn_sign_up.place(x=190, y=230)
# 创建两个按钮,command为按下后执行的命令

window.mainloop()
# 主窗口循环


图片素材: https://mofanpy.com/static/results/tkinter/3-01-02.gif

pickle.dump()参见: https://www.cnblogs.com/whiteprism/p/6201451.html

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mklpo147

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值