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)点击还原按钮没有反应代码应该怎么改
时间: 2023-07-19 09:55:25 浏览: 156
fatal error: boostdesc_bgm.i: No such file or directory补充文件
在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()
```
阅读全文