import re
import tkinter as tk
class PasswordStrength:
STRENGTHS = [
("无效的密码", "red"),
("非常弱的密码", "orange"),
("弱的密码", "yellow"),
("一般的密码", "green"),
("强的密码", "blue"),
("非常强的密码", "purple"),
]
def __init__(self, password):
self.password = password
self.value = self.calculate_strength()
self.text, self.color = self.STRENGTHS[self.value]
def calculate_strength(self):
strength = 0
if len(self.password) >= 8:
strength += 1
if re.search(r"\d", self.password):
strength += 1
if re.search(r"[a-z]", self.password):
strength += 1
if re.search(r"[A-Z]", self.password):
strength += 1
if re.search(r"[!@#$%^&*()_+-=]", self.password):
strength += 1
return strength
class GUI(tk.Frame):
def __init__(self, master):
super().__init__(master)
master.title("密码强度检测器")
master.geometry(
"400x200+{}+{}".format(
(master.winfo_screenwidth() - 400) // 2,
(master.winfo_screenheight() - 200) // 2,
)
)
master.resizable(False, False)
self.label = tk.Label(self, text="请输入密码:", font=("宋体", 16))
self.entry = tk.Entry(self, show="*", font=("宋体", 16))
self.entry.bind("<Return>", self.check)
self.button = tk.Button(self, text="检测", font=("宋体", 16))
self.button.bind("<Button-1>", self.check)
self.result = tk.Label(self, text=" ", font=("宋体", 16), fg="gray")
self.label.grid(row=0, column=0, padx=10, pady=10)
self.entry.grid(row=0, column=1, padx=10, pady=10)
self.button.grid(row=0, column=2, padx=10, pady=10)
self.result.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
self.pack()
def check(self, event):
password = self.entry.get()
strength = PasswordStrength(password)
self.result.config(text=strength.text, fg=strength.color)
root = tk.Tk()
gui = GUI(root)
root.mainloop()