py小项目:用户登录和注册系统开发

Tkinter窗口创建:

import tkinter as tk
import tkinter.messagebox
from PIL import Image, ImageTk
import pickle

win = tk.Tk()
win.title("欢迎")
win.geometry("600x400")

使用tkinter包创建并设置win窗口大小

图形元素加载与布局:

# 图片
image = Image.open("../连接测试/welcome.gif")
photo = ImageTk.PhotoImage(image)
label1 = tk.Label(win, image=photo)
label1.place(x=100, y=20)  # 设置图片位置

使用PIL中的Image,ImageTK包完成图片元素的加载并定位

# 用户名标签
t1 = tk.Label(win, text="用户名")
t1.place(x=138, y=200)  # 设置用户名标签位置
# 用户名输入框

itemE = tk.Entry(win, bd=2)
itemE.place(x=230, y=200)  # 设置用户名输入框位置
default_text = "example@python.com"
itemE.insert(0, default_text)

设置用户名标签并使用绝对定位,在使用tk.Entyry设置用户名输入框并用insert给定默认值

# 密码标签
t2 = tk.Label(win, text="密码")
t2.place(x=138, y=240)  # 设置密码标签位置
# 密码输入框
itemE1 = tk.Entry(win, bd=2,show='*')
itemE1.place(x=230, y=240)  # 设置密码输入框位置

与用户名设置同理,但是密码使用了show用户将输入变为*保护用户隐私

用户输入登录验证:

我实现了用户名和密码的输入验证逻辑,提高了系统的安全性。

def usr_login():
    usr_name = itemE.get()
    usr_pwd = itemE1.get()
    print(usr_name)
    try:
        with open('usrs_info.pickle', 'rb') as usr_file:
            print('1')
            usrs_info = pickle.load(usr_file)
            print(usrs_info)
    except FileNotFoundError:
        with open('usrs_info.pickle', 'wb') as usr_file:
            print('2')
            usrs_info = {'admin': 'admin'}
            pickle.dump(usrs_info, usr_file)


    print('ok')
    print('usr_name:',usr_name)

使用.get()将输入框里面的内容传给变量并打印用户名使用try与except判断用户名是否在usrs_info.pickle文件中,如果在就将用户名用pickle.load()从文件中读取字节流并反序列化为原始数据结构或对象到usrs_info并打印出来,如果文件中没有数据(即第一次运行程序或者文件被删除),会抛出FileNotFoundError异常,然后在except块中创建一个默认的管理员用户信息,并用pickle.dump()将数据序列化为字节流并写入文件到 pickle 文件中以字典的形式。

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, please try again.')
else:
    is_sign_up = tk.messagebox.askyesno(title='Welcome', message='You have not signed up yet. Sign up today.')
    if is_sign_up:
        usr_sign_up()

如果用户在原字典里面并且密码相等则弹出窗口欢迎,若密码不相等则弹出你的密码不正确请重新输入,如果用户名在原字典里面找不到则弹出你是否需要注册窗口并连接注册函数

注册功能:

def usr_sign_up():
    print('开始注册')
    def sign_up():
        nn = itemE2.get()
        np = itemE3.get()
        npf = itemE4.get()
        with open('usrs_info.pickle','rb')as usr_file:
            exist_usr_info = pickle.load(usr_file)
        if np != npf:
            tk.messagebox.showerror('错误','第一次密码与第二次密码不匹配')
        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('欢迎','你已经成功登入')

            up_win.destroy()

在外层函数中输入开始注册,在内层函数中分别设置三个变量分别储存用户名,新密码,与确认密码。用pickle.load(usr_file)将以前的信息反序列化到exist_usr_info以便后期我们添加新用户,如果新密码不等于确认密码则报错否则将信息序列化写入到usrs_info.pickle文件中并弹出欢迎窗口

设置注册窗口及登录与注册按钮

    # Add your sign-up logic here
    up_win = tk.Tk()
    up_win.title("欢迎")
    up_win.geometry("380x230")

    t3 = tk.Label(up_win, text="User name:")
    t3.place(x=30, y=20)
    itemE2 = tk.Entry(up_win, bd=2)
    itemE2.place(x=180, y=20)

    t4 = tk.Label(up_win, text="password:")
    t4.place(x=30, y=60)
    itemE3 = tk.Entry(up_win, bd=2)
    itemE3.place(x=180, y=60)

    t5 = tk.Label(up_win, text="Confirm password:")
    t5.place(x=30, y=100)
    itemE4 = tk.Entry(up_win, bd=2)
    itemE4.place(x=180, y=100)
    butt = tk.Button(up_win, text="注册",command=sign_up)
    butt.place(x=190, y=150)  # 设置注册按钮位置


# 登录按钮
butt = tk.Button(win, text="登录", command=usr_login)
butt.place(x=200, y=300)  # 设置登录按钮位置

# 注册按钮
butt1 = tk.Button(win, text="注册",command=usr_sign_up)
butt1.place(x=300, y=300)  # 设置注册按钮位置

win.mainloop()

标签及输入框的创建与上面的相同,使用tk.Button创建按钮并设置绝对定位

全部代码

import tkinter as tk
import tkinter.messagebox
from PIL import Image, ImageTk
import pickle

win = tk.Tk()
win.title("欢迎")
win.geometry("600x400")

# 图片
image = Image.open("../连接测试/welcome.gif")
photo = ImageTk.PhotoImage(image)
label1 = tk.Label(win, image=photo)
label1.place(x=100, y=20)  # 设置图片位置

# 用户名标签
t1 = tk.Label(win, text="用户名")
t1.place(x=138, y=200)  # 设置用户名标签位置

# 密码标签
t2 = tk.Label(win, text="密码")
t2.place(x=138, y=240)  # 设置密码标签位置

# 用户名输入框

itemE = tk.Entry(win, bd=2)
itemE.place(x=230, y=200)  # 设置用户名输入框位置
default_text = "example@python.com"
itemE.insert(0, default_text)

# 密码输入框
itemE1 = tk.Entry(win, bd=2,show='*')
itemE1.place(x=230, y=240)  # 设置密码输入框位置

def usr_login():
    usr_name = itemE.get()
    usr_pwd = itemE1.get()
    print(usr_name)
    try:
        with open('usrs_info.pickle', 'rb') as usr_file:
            print('1')
            usrs_info = pickle.load(usr_file)
            print(usrs_info)
    except FileNotFoundError:
        with open('usrs_info.pickle', 'wb') as usr_file:
            print('2')
            usrs_info = {'admin': 'admin'}
            pickle.dump(usrs_info, usr_file)


    print('ok')
    print('usr_name:',usr_name)

    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, please try again.')
    else:
        is_sign_up = tk.messagebox.askyesno(title='Welcome', message='You have not signed up yet. Sign up today.')
        if is_sign_up:
            usr_sign_up()

def usr_sign_up():
    print('开始注册')
    def sign_up():
        nn = itemE2.get()
        np = itemE3.get()
        npf = itemE4.get()
        with open('usrs_info.pickle','rb')as usr_file:
            exist_usr_info = pickle.load(usr_file)
        if np != npf:
            tk.messagebox.showerror('错误','第一次密码与第二次密码不匹配')
        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('欢迎','你已经成功登入')

            up_win.destroy()


    # Add your sign-up logic here
    up_win = tk.Tk()
    up_win.title("欢迎")
    up_win.geometry("380x230")

    t3 = tk.Label(up_win, text="User name:")
    t3.place(x=30, y=20)
    itemE2 = tk.Entry(up_win, bd=2)
    itemE2.place(x=180, y=20)

    t4 = tk.Label(up_win, text="password:")
    t4.place(x=30, y=60)
    itemE3 = tk.Entry(up_win, bd=2)
    itemE3.place(x=180, y=60)

    t5 = tk.Label(up_win, text="Confirm password:")
    t5.place(x=30, y=100)
    itemE4 = tk.Entry(up_win, bd=2)
    itemE4.place(x=180, y=100)
    butt = tk.Button(up_win, text="注册",command=sign_up)
    butt.place(x=190, y=150)  # 设置注册按钮位置


# 登录按钮
butt = tk.Button(win, text="登录", command=usr_login)
butt.place(x=200, y=300)  # 设置登录按钮位置

# 注册按钮
butt1 = tk.Button(win, text="注册",command=usr_sign_up)
butt1.place(x=300, y=300)  # 设置注册按钮位置

win.mainloop()

  • 16
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值