一段非常简单的密码强度检测Python源代码,功能过于简单重构了遍,让它起码有点质量能拿出来分享。
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()
- Python多线程截图工具 [2024-09-10]
- 密码强度检测Python源代码 [2023-06-06]
- 批量转存百度网盘分享资源Python脚本 [2022-09-29]
- Python一键安装卸载工具V1.0 [2022-07-21]
- Python+HTML随机点名抽奖源码模板 [2021-11-01]