1. 程式人生 > 其它 >Entry模擬對輸入的密碼進行驗證是否符合規範

Entry模擬對輸入的密碼進行驗證是否符合規範

技術標籤:python

需求:
1、密碼必須不少於6位
2、密碼不能包含中文
3、密碼不能有特殊字元
4、密碼裡至少有一個大寫字母

效果如圖所示:
在這裡插入圖片描述
完整程式碼如下:

from tkinter import *
import re

root = Tk()
root.title("密碼動態驗證")
root.geometry("350x150")
root.resizable(0, 0)
f1 = Frame(root, pady=35)
f1.pack()


def reg2(password):
    zh = re.compile('[\u4e00-\u9fa5(\\W)]'
) # \W表示匹配任何非Unicode的單詞字元相當於[^a-zA-Z0-9]中文(\u4e00-\u9fa5) match = zh.findall(password) capital_letters = re.compile('[A-Z]+') match1 = capital_letters.findall(password) if match: match = re.sub(r"\['", "", match[0]) match = re.sub(r"\']", ""
, match[0]) tishi.set('有中文或特殊字元:%s' % match) print('有中文或特殊字元:%s' % match) return False elif len(password) < 6: tishi.set("密碼不能小於6位") print("密碼不能小於6位") elif not match1: tishi.set("請至少輸入一個大寫字母") print("請至少輸入一個大寫字母"
) else: tishi.set("當前符合條件密碼:" + password) print("當前符合條件密碼:" + password) return True tishi = StringVar() reg = f1.register(reg2) l1 = Label(f1, text="密 碼:").grid(row=0, column=0) e1 = Entry(f1, validate="key", show='*', validatecommand=(reg, '%P')) e1.grid(row=0, column=1) Label(f1, textvariable=tishi,wraplength=300).grid(row=1,column=0,columnspan=3) root.mainloop()