一个简单的登陆系统

# View more python learning tutorial on my Youtube and Youku channel!!!

# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial

import tkinter as tk
import pickle
from tkinter.messagebox import *

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

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

# user information
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_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)

#检查数据库是否存在
def check_file():
    try:
        with open('users_info.pickle','rb') as users_file:
            usrs_info = pickle.load(users_file)
    except FileNotFoundError:
        with open('users_info.pickle','wb') as users_file:
            usrs_info = {'admin': 'admin'}
            pickle.dump(usrs_info, users_file)
            print('初始化数据成功!')
    return usrs_info
def usr_login():
    name = entry_usr_name.get()
    password = entry_usr_pwd.get()
    usrs_info = check_file()
    print('usrs_info:',usrs_info)
    if name in usrs_info:
        if password == usrs_info[name]:
            showinfo(title='Welcome', message='How are you? ' +
                     name +',welcome to 何清林 python!')
            window.destroy()
        else:
            showerror(message = '密码错误!')
    else:
        yes = askyesno(message='你还没有注册账号,是否注册?')
        print('yes:',yes)
        if yes:
            usr_sign_up()
def usr_sign_up():
    def create_account():
        sign_name = entry_usr_name.get()
        sign_password = entry_usr_pwd.get()
        sign_password_crmt = var_usr_pwd_crmt.get()
        print("sign_name:",sign_name,
              "sign_password:",sign_password,
              "sign_password_crmt:",sign_password_crmt)
        if sign_password_crmt != sign_password:
            showinfo(title = "提示",message = "密码和确认密码不一致!")
        else:
            usrs_info = check_file()
            with open('users_info.pickle','rb') as users_file:
                usrs_info = pickle.load(users_file)
                if sign_name in usrs_info:
                    showinfo(title = "提示",message = sign_name +'已经被人注册过了')
                else:
                    with open('users_info.pickle','wb') as users_file:
                        usrs_info[sign_name] = sign_password
                        pickle.dump(usrs_info, users_file)
                        window_sign_up.destroy()
                        showinfo(title = "提示",message = '注册会员成功')
    window_sign_up = tk.Toplevel(window)
    window_sign_up.title("注册")
    window_sign_up.geometry("350x200")
    tk.Label(window_sign_up, text='User name: ').place(x=30, y= 20)
    tk.Label(window_sign_up, text='Password: ').place(x=30, y= 60)
    tk.Label(window_sign_up, text='Password comfirt: ').place(x=30, y= 100)

    var_usr_name = tk.StringVar()
    var_usr_name.set('example@python.com')
    entry_usr_name = tk.Entry(window_sign_up, textvariable=var_usr_name)
    entry_usr_name.place(x=140, y=20)
    var_usr_pwd = tk.StringVar()
    entry_usr_pwd = tk.Entry(window_sign_up, textvariable=var_usr_pwd, show='*')
    entry_usr_pwd.place(x=140, y=60)
    var_usr_pwd_crmt = tk.StringVar()
    entry_usr_pwd_crmt = tk.Entry(window_sign_up, textvariable=var_usr_pwd_crmt, show='*')
    entry_usr_pwd_crmt.place(x=140, y=100)
    btn_login = tk.Button(window_sign_up, text='注册', command=create_account)
    btn_login.place(x=180, y=140)

# login and sign up button
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)

window.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值