为下列代码添加注释:import re import tkinter as tk from tkinter import messagebox import hashlib import os def zhuce(): a = username_entry.get() b = password_entry.get() if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]{4,19}$", a): messagebox.showerror("错误", "用户名长度为5-20个字符,只能包含数字字母下划线,且必须以字母开头") return if not re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,16}$', b): messagebox.showerror("错误", "密码长度为8-16个字符,且包含有至少一个小写字母、一个大写字母和一个数字") return if os.path.exists("user.txt"): with open("user.txt", "r") as f: users = f.readlines() for user in users: if user.split(':')[0] == a: messagebox.showerror("错误", "用户名已存在!") return hashed_password = hashlib.md5(b.encode()).hexdigest() with open("user.txt", "a") as f: f.write(f"{a}:{hashed_password}\n") messagebox.showinfo("成功", "注册成功!") def denglu(): a = username_entry.get() b = password_entry.get() password = hashlib.md5(b.encode()).hexdigest() if not os.path.exists("user.txt"): messagebox.showerror("错误", "用户名不存在!") return with open("user.txt", "r") as f: users = f.readlines() for user in users: if user.split(':')[0] == a: if user.split(':')[1].strip() == password: messagebox.showinfo("成功", "登录成功!") return else: messagebox.showerror("错误", "密码错误!") return messagebox.showerror("错误", "用户名不存在!") root = tk.Tk() username_label = tk.Label(root, text="用户名:") username_label.pack() username_entry = tk.Entry(root) username_entry.pack() password_label = tk.Label(root, text="密码:") password_label.pack() password_entry = tk.Entry(root, show="*") password_entry.pack() login_button = tk.Button(root, text="登录", command=denglu) login_button.pack() register_button = tk.Button(root, text="注册", command=zhuce) register_button.pack() root.mainloop()
时间: 2023-06-24 10:06:41 浏览: 85
# 导入所需模块
import re
import tkinter as tk
from tkinter import messagebox
import hashlib
import os
# 定义注册功能
def zhuce():
# 获取用户名和密码
a = username_entry.get()
b = password_entry.get()
# 验证用户名是否合法
if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]{4,19}$", a):
messagebox.showerror("错误", "用户名长度为5-20个字符,只能包含数字字母下划线,且必须以字母开头")
return
# 验证密码是否合法
if not re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,16}$', b):
messagebox.showerror("错误", "密码长度为8-16个字符,且包含有至少一个小写字母、一个大写字母和一个数字")
return
# 检查用户是否已存在
if os.path.exists("user.txt"):
with open("user.txt", "r") as f:
users = f.readlines()
for user in users:
if user.split(':')[0] == a:
messagebox.showerror("错误", "用户名已存在!")
return
# 将密码进行哈希加密并写入文件
hashed_password = hashlib.md5(b.encode()).hexdigest()
with open("user.txt", "a") as f:
f.write(f"{a}:{hashed_password}\n")
# 弹出注册成功提示框
messagebox.showinfo("成功", "注册成功!")
# 定义登录功能
def denglu():
# 获取用户名和密码
a = username_entry.get()
b = password_entry.get()
# 将密码进行哈希加密并与文件中的信息进行比对
password = hashlib.md5(b.encode()).hexdigest()
if not os.path.exists("user.txt"):
messagebox.showerror("错误", "用户名不存在!")
return
with open("user.txt", "r") as f:
users = f.readlines()
for user in users:
if user.split(':')[0] == a:
if user.split(':')[1].strip() == password:
# 弹出登录成功提示框
messagebox.showinfo("成功", "登录成功!")
return
else:
# 弹出密码错误提示框
messagebox.showerror("错误", "密码错误!")
return
# 弹出用户名不存在提示框
messagebox.showerror("错误", "用户名不存在!")
# 创建主窗口并添加控件
root = tk.Tk()
username_label = tk.Label(root, text="用户名:")
username_label.pack()
username_entry = tk.Entry(root)
username_entry.pack()
password_label = tk.Label(root, text="密码:")
password_label.pack()
password_entry = tk.Entry(root, show="*")
password_entry.pack()
login_button = tk.Button(root, text="登录", command=denglu)
login_button.pack()
register_button = tk.Button(root, text="注册", command=zhuce)
register_button.pack()
# 进入主事件循环
root.mainloop()
阅读全文