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=140) 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=10, y=250) def tihuan(phone_input, id_input,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(data1,data2): # 匹配手机号和身份证号码 pattern = re.compile(r'1\d{10}|\d{17}[\dXx]') # 替换所有匹配的敏感信息 desensitized_data = re.sub(pattern, replace_sensitive_info, data1,data2) return desensitized_data # 测试数据脱敏函数 data1 = phone_input data2 = id_input desensitized_data = desensitize_data(data1,data2) a1=tk.Label(root,text=("手机号:",desensitized_data(data1)),font=("微软雅黑 -20")) a1.place(x=10,y=300) a2=tk.Label(root,text=("身份证号:",desensitized_data(data2)),font=("微软雅黑 -20")) a2.place(x=10,y=400)报错'str' object cannot be interpreted as an integer
时间: 2024-03-07 14:53:44 浏览: 28
这个报错是因为在下面这行代码中,将字符串类型的 `data1` 和 `data2` 作为参数传递给了 `desensitize_data` 函数,但是 `desensitize_data` 函数的参数要求是整数类型的:
```
desensitized_data = desensitize_data(data1,data2)
```
你需要将 `frame_phone_input.get()` 和 `frame_id_input.get()` 返回的字符串类型转换为整数类型,或者改变 `desensitize_data` 函数的参数要求。
相关问题
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())) 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)报错root 没有被定义怎么改
在 choose() 函数中创建的 root 对象是局部变量,只能在该函数中使用,因此在 tihuan() 函数中无法访问。要解决这个问题,可以将 root 对象作为参数传递给 tihuan() 函数。修改后的代码如下:
```
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)
email_input = tk.StringVar()
frame_email_input = tk.Entry(root, textvariable=email_input)
frame_email_input.place(x=75, y=148,height=20,width=120)
tk.Label(root, text="出生日期:",font=("微软雅黑 -20")).place(x=10, y=180)
date_input = tk.StringVar()
frame_date_input = tk.Entry(root, textvariable=date_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, 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)
```
def choose(): window=tk.Tk() window.title("菜单") window.geometry("1000x750") tk.Label(window, text="数据脱敏系统", font=("微软雅黑 -30"),activebackground='pink').place(x=400, y=20) tk.Label(window, text="(请选择)", font=("黑体", 14)).place(x=450, y=60) table_2=tk.Button(window, text="对表格中数据进行脱敏", font=("微软雅黑 -30"),bg='pink',command=table2) table_2.place(x=100, y=330)def table2(): root = tk.Tk()# 这是主窗口,也就是所说的初始界面 window = tk.Toplevel() root.title("欢迎使用本系统") # 窗口名 root.geometry("1000x750") # 窗口大小 # 手机号、身份证号、年龄、出生日期 data = [ {"手机号": "18845298308", "身份证号": "110101199001011234", "年龄": 31, "出生日期": "1990-01-01"}, {"手机号": "13836277378", "身份证号": "110101199002022345", "年龄": 20, "出生日期": "2002-10-25"}]def desensitize_data(): pattern = re.compile(r'1\d{10}|\d{17}[\dXx]') new_data = data.copy() for i in range(len(data)): for key in data[i]: if isinstance(data[i][key], str): new_data[i][key] = re.sub(pattern, replace_sensitive_info, data[i][key]) return new_data 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 replace_mask(): global data data = desensitize_data() show_data() def show_original_data(): global data data = [ {"手机号": "18845298308","身份证号": "110101199001011234", "年龄": 31, "出生日期": "1990-01-01"}, {"手机号": "13836277378", "身份证号": "110101199002022345", "年龄": 20, "出生日期": "2002-10-25"}]show_data() replace_mask_btn = Button(root, text="替换脱敏", font=("微软雅黑 -20"),bg='pink',command=replace_mask) replace_mask_btn.place(x=600,y=20) replace_unmask_btn = Button(root, text="还原", font=("微软雅黑 -20"),bg='pink', command=show_original_data) replace_unmask_btn.place(x=180,y=420)点击还原按钮没有反应代码应该怎么改
在show_original_data()函数中,应该在函数内部添加一个展示数据的操作,例如在函数内部添加一个show_data()函数,用于展示数据。修改后的代码如下:
```
def show_original_data():
global data
data = [
{"手机号": "18845298308","身份证号": "110101199001011234", "年龄": 31, "出生日期": "1990-01-01"},
{"手机号": "13836277378", "身份证号": "110101199002022345", "年龄": 20, "出生日期": "2002-10-25"}
]
show_data()
def show_data():
for i in range(len(data)):
for key in data[i]:
tk.Label(window, text=data[i][key], font=("微软雅黑 -20")).place(x=200+i*200, y=200+40*list(data[i].keys()).index(key))
```
同时,在choose()函数中,需要将root改为window,防止两个窗口混淆。修改后的代码如下:
```
def choose():
window=tk.Tk()
window.title("菜单")
window.geometry("1000x750")
tk.Label(window, text="数据脱敏系统", font=("微软雅黑 -30"),activebackground='pink').place(x=400, y=20)
tk.Label(window, text="(请选择)", font=("黑体", 14)).place(x=450, y=60)
table_2=tk.Button(window, text="对表格中数据进行脱敏", font=("微软雅黑 -30"),bg='pink',command=table2)
table_2.place(x=100, y=330)
def table2():
window = tk.Toplevel()
window.title("欢迎使用本系统")
window.geometry("1000x750")
# 手机号、身份证号、年龄、出生日期
data = [
{"手机号": "18845298308", "身份证号": "110101199001011234", "年龄": 31, "出生日期": "1990-01-01"},
{"手机号": "13836277378", "身份证号": "110101199002022345", "年龄": 20, "出生日期": "2002-10-25"}
]
def desensitize_data():
pattern = re.compile(r'1\d{10}|\d{17}[\dXx]')
new_data = data.copy()
for i in range(len(data)):
for key in data[i]:
if isinstance(data[i][key], str):
new_data[i][key] = re.sub(pattern, replace_sensitive_info, data[i][key])
return new_data
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 replace_mask():
nonlocal data
data = desensitize_data()
show_data()
def show_original_data():
nonlocal data
data = [
{"手机号": "18845298308","身份证号": "110101199001011234", "年龄": 31, "出生日期": "1990-01-01"},
{"手机号": "13836277378", "身份证号": "110101199002022345", "年龄": 20, "出生日期": "2002-10-25"}
]
show_data()
def show_data():
for i in range(len(data)):
for key in data[i]:
tk.Label(window, text=data[i][key], font=("微软雅黑 -20")).place(x=200+i*200, y=200+40*list(data[i].keys()).index(key))
replace_mask_btn = tk.Button(window, text="替换脱敏", font=("微软雅黑 -20"),bg='pink',command=replace_mask)
replace_mask_btn.place(x=600,y=20)
replace_unmask_btn = tk.Button(window, text="还原", font=("微软雅黑 -20"),bg='pink', command=show_original_data)
replace_unmask_btn.place(x=180,y=420)
show_data()
```
相关推荐
![png](https://img-home.csdnimg.cn/images/20210720083516.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)