def table2(): window = tk.Tk()# 这是主窗口,也就是所说的初始界面 window = tk.Toplevel() window.title("欢迎使用本系统") # 窗口名 window.geometry("1000x750") # 窗口大小 # 手机号、身份证号、年龄、出生日期 data = [ {"手机号": "18845298308", "身份证号": "110101199001011234", "年龄": 31, "出生日期": "1990-01-01"}, {"手机号": "13836277378", "身份证号": "110101199002022345", "年龄": 20, "出生日期": "2002-10-25"}]def show_original_data(): global data data = [ {"手机号": "18845298308","身份证号": "110101199001011234", "年龄": 31, "出生日期": "1990-01-01"}, {"手机号": "13836277378", "身份证号": "110101199002022345", "年龄": 20, "出生日期": "2002-10-25"}]show_data()replace_unmask_btn = Button(window, text="还原", font=("微软雅黑 -20"),bg='pink', command=show_original_data) replace_unmask_btn.place(x=180,y=420)def show_data(): # 清空列表框 listbox.delete(0, END) # 显示表头 listbox.insert(END, "{:<15s}{:<25s}{:<5s}{}".format("手机号", "身份证号", "年龄", "出生日期")) # 显示数据 for i in range(len(data)): listbox.insert(END, "{:<15s}{:<25s}{:<5d}{}".format( data[i]["手机号"], data[i]["身份证号"], data[i]["年龄"], data[i]["出生日期"] )) # 创建列表框 listbox = Listbox(window, width=60, height=20) listbox.grid(row=0, column=0, columnspan=4) # 显示数据 show_data()点击还原的按钮没有反应,不能更新列表框中的数据是怎么回事
时间: 2023-07-19 18:55:14 浏览: 25
根据你提供的代码,我发现在 show_original_data() 函数中虽然改变了全局变量 data 的值,但是没有调用 show_data() 函数来更新列表框中的数据。因此,点击还原的按钮没有反应,不能更新列表框中的数据。你需要在 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_excel(self): # 清空第一个表格内容 self.result_text.delete(1.0, tk.END) # 清空第二个表格内容 if hasattr(self, 'table_frame2'): self.table_frame2.destroy() self.table_frame2 = tk.Frame(self.result_text2) self.table_frame2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) table_scroll_y2 = ttk.Scrollbar(self.table_frame2, orient=tk.VERTICAL) table_scroll_y2.pack(side=tk.RIGHT, fill=tk.Y) table_scroll_x2 = ttk.Scrollbar(self.table_frame2, orient=tk.HORIZONTAL) table_scroll_x2.pack(side=tk.BOTTOM, fill=tk.X) # 清空第二个表格内容 if hasattr(self, 'table_frame'): self.table_frame.destroy() self.table_frame = tk.Frame(self.result_text) self.table_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) table_scroll_y = ttk.Scrollbar(self.table_frame, orient=tk.VERTICAL) table_scroll_y.pack(side=tk.RIGHT, fill=tk.Y) table_scroll_x = ttk.Scrollbar(self.table_frame, orient=tk.HORIZONTAL) table_scroll_x.pack(side=tk.BOTTOM, fill=tk.X) # 显示第一个表格 header = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True)) # 创建表格 table = ttk.Treeview(self.table_frame, columns=header, show='headings',
height=10, yscrollcommand=table_scroll_y.set, xscrollcommand=table_scroll_x.set)
# 设置表格样式
table.column("#0", width=0, stretch=tk.NO)
for i, col in enumerate(header):
table.column(col, width=100, stretch=tk.NO)
table.heading(col, text=col)
# 填充表格数据
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
table.insert("", tk.END, values=row)
# 显示第二个表格
header2 = next(self.record_sheet2.iter_rows(min_row=1, max_row=1, values_only=True)) # 创建表格
table2 = ttk.Treeview(self.table_frame2, columns=header2, show='headings', height=10, yscrollcommand=table_scroll_y2.set, xscrollcommand=table_scroll_x2.set)
# 设置表格样式
table2.column("#0", width=0, stretch=tk.NO)
for i, col in enumerate(header2):
table2.column(col, width=100, stretch=tk.NO)
table2.heading(col, text=col)
# 填充表格数据
for row in self.record_sheet2.iter_rows(min_row=2, values_only=True):
table2.insert("", tk.END, values=row)
# 显示表格
table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y.config(command=table.yview)
table_scroll_x.config(command=table.xview)
table2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y2.config(command=table2.yview)
table_scroll_x2.config(command=table2.xview)
self.result_text.config(state=tk.NORMAL)
self.result_text.insert(tk.END, "Excel表格读取完成!\n")
self.result_text.config(state=tk.DISABLED)
这段代码是用来读取 Excel 表格并在界面上显示的。其中,通过 Treeview 控件实现了表格的显示,使用 Scrollbar 实现了滚动条。同时,还支持同时显示两个表格。
# 查询窗口 class QueryWindow: def __init__(self, master): self.master = master self.master.title("查詢窗口") self.master.state('zoomed') # 窗口最大化 # 创建左侧面板 self.panel_left = tk.Frame(master, width=150, bg='lightcyan') self.panel_left.pack(side=tk.LEFT, fill=tk.Y) # 创建右侧面板 self.panel_right = tk.Frame(master, width=20, bg='lightcyan') self.panel_right.pack(side=tk.RIGHT, fill=tk.Y) # 创建工具栏 self.toolbar = tk.Frame(master,width=20, bg='white') self.toolbar.pack(side=tk.BOTTOM, fill=tk.X)# 创建 Frame 来包含标签和下拉框 input_frame = tk.Frame(self.panel_left, padx=0, pady=5) input_frame.pack(side=tk.TOP, fill=tk.X)# 创建右侧面板 self.panel_right = tk.Frame(master, bg='white') self.panel_right.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 创建文本框1 text_frame = tk.Frame(self.panel_right) text_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True) self.result_text = tk.Text(text_frame) self.result_text.pack(side=tk.TOP, fill=tk.BOTH, expand=True) # 创建文本框2 text_frame2 = tk.Frame(self.panel_right) text_frame2.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) self.result_text2 = tk.Text(text_frame2) self.result_text2.pack(side=tk.TOP, fill=tk.BOTH, expand=True)在這個窗口中在添加一個放到下面的工具欄
要在这个窗口中添加一个放到下面的工具栏,可以参考以下代码:
```
# 创建底部工具栏
bottom_toolbar = tk.Frame(master, bg='white')
bottom_toolbar.pack(side=tk.BOTTOM, fill=tk.X)
# 添加工具栏按钮
button1 = tk.Button(bottom_toolbar, text='按钮1')
button1.pack(side=tk.LEFT, padx=5, pady=5)
button2 = tk.Button(bottom_toolbar, text='按钮2')
button2.pack(side=tk.LEFT, padx=5, pady=5)
# 添加分隔符
separator = tk.Frame(bottom_toolbar, height=20, width=1, bg='gray')
separator.pack(side=tk.LEFT, padx=5, pady=5)
# 添加下拉框
options = ['选项1', '选项2', '选项3']
selected_option = tk.StringVar()
selected_option.set(options[0])
dropdown = tk.OptionMenu(bottom_toolbar, selected_option, *options)
dropdown.pack(side=tk.LEFT, padx=5, pady=5)
```
将以上代码放在 `__init__` 方法中的合适位置即可。记得根据需要调整工具栏按钮、分隔符和下拉框的样式和功能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)