def choose(): root=tk.Tk() root.title("数据脱敏") root.geometry("1000x750") tk.Label(root, text="请输入想要脱敏的信息:", font=("微软雅黑 -30")).place(x=10, y=15) tk.Label(root, text="手机号:",font=("微软雅黑 -20")).place(x=10, y=60) phone_input=tk.StringVar() frame_phone_input=tk.Entry(root, textvariable=phone_input) frame_phone_input.place(x=90, y=68,height=20,width=120) tk.Label(root, text="身份证号:",font=("微软雅黑 -20")).place(x=10, y=100) id_input=tk.StringVar() frame_id_input=tk.Entry(root, textvariable=id_input) frame_id_input.place(x=110, y=108,height=20,width=120) tk.Label(root, text="邮箱:",font=("微软雅黑 -20")).place(x=10, y=140) id_input=tk.StringVar() frame_youxiang_input=tk.Entry(root, textvariable=id_input) frame_youxiang_input.place(x=75, y=148,height=20,width=120) tk.Label(root, text="出生日期:",font=("微软雅黑 -20")).place(x=10, y=180) id_input=tk.StringVar() frame_date_input=tk.Entry(root, textvariable=id_input) frame_date_input.place(x=110, y=188,height=20,width=120) btn1 = tk.Button(root, text="替换", font=("微软雅黑 -20"),bg='pink',command=lambda: tihuan(frame_phone_input.get(), frame_id_input.get(), root)) btn1.place(x=600, y=60) def tihuan(phone_input, id_input): # 替换手机号和身份证号码的函数 def replace_sensitive_info(match): sensitive_info = match.group(0) if re.match(r'^1\d{10}$', sensitive_info): # 匹配手机号 return sensitive_info[0:3] + 'aaaa' + sensitive_info[7:] elif re.match(r'^\d{17}[\dXx]$', sensitive_info): # 匹配身份证号 return sensitive_info[0:8] + 'aaaaaaaa' + sensitive_info[16:] else: return sensitive_info # 数据脱敏函数 def desensitize_data(data): # 匹配手机号和身份证号码 pattern = re.compile(r'1\d{10}|\d{17}[\dXx]') # 替换所有匹配的敏感信息 desensitized_data = re.sub(pattern, replace_sensitive_info, data) return desensitized_data # 测试数据脱敏函数 data = phone_input + id_input desensitized_data = desensitize_data(data) a1=tk.Label(root,text=("脱敏后的数据",desensitized_data),font=("微软雅黑 -20")) a1.place(x=590,y=300)报错tihuan() takes 2 positional arguments but 3 were given
时间: 2023-12-16 11:03:41 浏览: 36
这是因为在调用 `tihuan()` 函数时,你传入了三个参数 `frame_phone_input.get()`,`frame_id_input.get()` 和 `root`,但是在函数定义时只定义了两个参数 `phone_input` 和 `id_input`。如果你需要在函数中使用 `root` 参数,你需要在函数定义时添加该参数并在函数调用时传入。例如:
```
def tihuan(phone_input, id_input, root): # 添加 root 参数
# 替换手机号和身份证号码的函数
def replace_sensitive_info(match):
sensitive_info = match.group(0)
if re.match(r'^1\d{10}$', sensitive_info): # 匹配手机号
return sensitive_info[0:3] + 'aaaa' + sensitive_info[7:]
elif re.match(r'^\d{17}[\dXx]$', sensitive_info): # 匹配身份证号
return sensitive_info[0:8] + 'aaaaaaaa' + sensitive_info[16:]
else:
return sensitive_info
# 数据脱敏函数
def desensitize_data(data):
# 匹配手机号和身份证号码
pattern = re.compile(r'1\d{10}|\d{17}[\dXx]')
# 替换所有匹配的敏感信息
desensitized_data = re.sub(pattern, replace_sensitive_info, data)
return desensitized_data
# 测试数据脱敏函数
data = phone_input + id_input
desensitized_data = desensitize_data(data)
a1 = tk.Label(root, text=("脱敏后的数据", desensitized_data), font=("微软雅黑 -20"))
a1.place(x=590, y=300)
# 在调用 tihuan() 函数时传入 root 参数
btn1 = tk.Button(root, text="替换", font=("微软雅黑 -20"), bg='pink', command=lambda: tihuan(frame_phone_input.get(), frame_id_input.get(), root))
btn1.place(x=600, y=60)
```