Tkinter注册界面判断用户名是否唯一的合法性验证(Python3.7)

前言:
在上一篇的基础上点击查看上一篇内容,增加判断用户名是否存在,若存在则弹出对话框,若不存在则注册。如果不加以判断会发生什么,请看下图,这是账户信息中已注册的两个账号:
在这里插入图片描述
假如我们再注册一个账号,用户名也叫李四,密码为 lisi1234,看下会发生什么。我们看到账户信息里出现了两条用户名相同,但密码不同的账户。
在这里插入图片描述
这里要注意的是,虽然这样可以注册成功,但是一但到登录的环节就出现问题了,我们知道字典的键是唯一的,假如字典中有两个相同的键,打印输出的时候只会输出最后一个相同的键值对,举例说明

DIC = {“张三”:“123456”,“李四”:“abc123”,“李四”:“lisi1234”}
print(DIC)
输出结果:{“张三”:“123456”,“李四”:“lisi1234”}

这就是为什么用户注册账号时要判断用户名是否已经被注册的原因,那么我们就看下这个判断是否存在的代码,在上一篇给出的代码register方法里加入了以下内容,仅此而已,但要注意elif的缩进,其他的判断要加在username.get()不为空下面:

    '''只有当用户名不为空才需要做判断'''
    elif username.get() != "":
    '''已只读方式打开文件'''
        f = open("D:\\账户信息.txt", mode="r", encoding="utf-8")
        '''逐行读取'''
        information = f.readlines()
        DIC = {
    
    } #创建空字典
        for i in range(len(information)): #遍历每一条数据存入字典
            DIC[information[i].split("\t")[0]] = information[i].split("\t")[1][0:-1]
        if username.get() in DIC.keys(): #判断用户名在字典的键里
            messagebox.showerror("警告",message="当前用户名: {0}\n已被注册,请更换".format(username.get()))

看到没,和第一条的键重复了
在这里插入图片描述

永远记着思路比结果重要,最后附上完整代码:

from tkinter import *
from tkinter import messagebox
import hashlib

root = Tk()
root.title("注册窗口演示")
root.geometry("400x250")
root.resizable(0, 0)
f1 = Frame(root)
f1.pack()
l1 = Label(f1, text="用户名").grid(row=0, column=0)
l2 = Label(f1, text="输入密码").grid(row=1, column=0)
l2 = Label(f1, text="再次确认").grid(row=2, column=0)


def change():
    username.config(bg="white")
    return True


def change2():
    password.config(bg="white")
    return True


def change3():
    password_.config(bg="white")
    return True


username = Entry(f1, validate="focusin", validatecommand=change)
username.grid(row=0, column=1, pady=20)
password = Entry(f1, validate="focusin", validatecommand=change2)
password.grid(row=1, column=1)
password_ = Entry(f1, validate="focusin", validatecommand=change3)
password_.grid(row=2, column=1)


def register():
    if username.get() == "":
        username.config(bg="Crimson")
        messagebox.showerror("提示", "用户名不能为空")
    elif username.get() != "":
        f = open("D:\\账户信息.txt", mode="r", encoding="utf-8")
        information = f.readlines()
        DIC = {
    
    }
        for i in range(len(information)):
            DIC[information[i].split("\t")[0]] = information[i].split("\t")[1][0:-1]
        if username.get() in DIC.keys():
            messagebox.showerror("警告",message="当前用户名: {0}\n已被注册,请更换".format(username.get()))
        elif password.get() == "":
            messagebox.showerror("提示", "密码不能为空")
        elif password.get() != password_.get():
            password.config(bg="Crimson")
            password_.config(bg="Crimson")
            password_.delete(0, END)
            messagebox.showerror("提示", "两次密码输入不一致")
        elif password.get() == password_.get():
            m = hashlib.md5("欢乐海岸".encode("utf-8"))
            m.update(password.get().encode("utf-8"))
            f = open("D:\\账户信息.txt", mode="a", encoding="utf-8")
            f.write(username.get() + "\t" + m.hexdigest() + "\n")
            print(m.hexdigest())
            f.close()
            messagebox.showinfo("提示", "注册成功")


Button(f1, text="注册", width=10, command=register).grid(row=3, column=0, sticky=W, pady=5)
Button(f1, text="退出", width=10, command=root.quit).grid(row=3, column=1, sticky=E, pady=5)


def readinfo():
    f = open("D:\\账户信息.txt", mode="r", encoding="utf-8")
    information = f.readlines()

    D = {
    
    }

    for i in range(len(information)):
        D[information[i].split("\t")[0]] = information[i].split("\t")[1][0:-1]

    print(D)

    f.close()


Button(f1, text="读出账户信息", width=10, command=readinfo).grid(row=4, column=1, sticky=E, pady=5)

root.mainloop()

猜你喜欢

转载自blog.csdn.net/weixin_51424938/article/details/111877001