如何在不重启程序时更新读取TXT的CustomTkinter按钮
解决CustomTkinter删除条目后即时更新按钮的问题
你的核心问题是只修改了文件但没同步更新GUI,而且没有保存已创建按钮的引用,没法直接定位并销毁目标按钮。下面是具体的修改方案:
关键修改步骤
1. 新增按钮引用存储
在你的类初始化方法里,添加一个字典用来保存每个按钮的引用,关联对应的行号:
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 其他初始化代码... # 键为行号,值为(Current Apps按钮, Remove Apps按钮)的元组 self.app_buttons = {}
2. 抽离按钮加载逻辑为独立函数
把原来add_app里创建按钮的代码单独抽成load_app_buttons函数,负责清空旧按钮、从文件重新加载并创建新按钮:
def load_app_buttons(self): # 销毁所有已存在的按钮 for current_btn, remove_btn in self.app_buttons.values(): current_btn.destroy() remove_btn.destroy() self.app_buttons.clear() # 重新读取文件创建按钮 with open('ez_start_path_file.txt', 'r') as infile: line_num = 0 for line in infile: line_num += 1 # 匹配ID行的应用名称 match = re.search(r'.*?\[(.*)].*', line) if match: app_name = match.group(1) # 创建Current Apps标签页的按钮 current_btn = customtkinter.CTkButton( self.tabview.tab("Current Apps"), text=app_name, command=lambda rw=line_num: self.start_app(rw) ) # 创建Remove Apps标签页的按钮 remove_btn = customtkinter.CTkButton( self.tabview.tab("Remove Apps"), text=app_name, command=lambda rw=line_num: self.remove_app(rw) ) # 确定按钮的网格位置 if line_num <= 15: row = line_num col = 0 # 配置单列布局 self.tabview.tab("Current Apps").grid_columnconfigure(0, weight=1) self.tabview.tab("Remove Apps").grid_columnconfigure(0, weight=1) elif line_num > 16 and line_num <= 32: row = line_num - 16 col = 1 # 配置双列布局 self.tabview.tab("Current Apps").grid_columnconfigure((0, 1), weight=1) self.tabview.tab("Remove Apps").grid_columnconfigure((0, 1), weight=1) elif line_num > 32: row = line_num - 32 col = 2 # 配置三列布局 self.tabview.tab("Current Apps").grid_columnconfigure((0, 1, 2), weight=1) self.tabview.tab("Remove Apps").grid_columnconfigure((0, 1, 2), weight=1) # 放置按钮 current_btn.grid(row=row, column=col, padx=15, pady=15) remove_btn.grid(row=row, column=col, padx=15, pady=15) # 保存按钮引用 self.app_buttons[line_num] = (current_btn, remove_btn)
3. 修改add_app函数
删掉原来创建按钮的代码,换成调用load_app_buttons刷新GUI:
def add_app(self): path_location = app_path.get() path_name = app_name.get() # 清空输入框 self.entry_add_apps_name.delete(0, "end") self.entry_add_apps_path.delete(0, "end") # 检查文件行数是否达到上限 with open('ez_start_path_file.txt') as infile: line_count = sum(1 for _ in infile) if line_count >= 64: notice_label = customtkinter.CTkLabel( self.tabview.tab("Add Apps"), text="已达到最大应用数量限制" ) notice_label.grid(row=8, column=0) self.after(2500, notice_label.destroy()) return # 检查输入是否为空 if not path_location or not path_name: print('字段不能为空') return # 写入文件 with open("ez_start_path_file.txt", "a") as f: f.write(f'ID=[{path_name}]\nPATH={path_location}\n') # 显示添加成功提示 confirm_label = customtkinter.CTkLabel( self.tabview.tab("Add Apps"), text="添加成功!" ) confirm_label.grid(row=8, column=0) self.after(2500, confirm_label.destroy()) # 刷新按钮列表 self.load_app_buttons()
4. 修改remove_app函数
在删除文件内容后,立即调用load_app_buttons刷新GUI:
def remove_app(self, rw): with open('ez_start_path_file.txt', 'r') as fr: lines = fr.readlines() with open('ez_start_path_file.txt', 'w') as fw: ptr = 1 for line in lines: if ptr != rw and ptr != rw + 1: fw.write(line) ptr += 1 # 删除后立即刷新按钮 self.load_app_buttons()
修改说明
- 通过
app_buttons字典保存按钮引用,确保刷新时能彻底销毁旧按钮,避免重复创建 - 抽离独立的加载函数后,不管是添加还是删除操作,只要文件内容变化,调用一次就能同步更新GUI
- 删除操作后直接触发刷新,实现"删除文件条目后按钮即时消失"的效果
内容的提问来源于stack exchange,提问作者john jacobs
相关产品推荐
相关产品推荐

