|
分类排序
def _show_navigation_context_menu(
self, event: tk.Event, button: ttk.Button, section: str
):
"""显示导航栏右键菜单"""
self.selected_nav_button = button
self.selected_nav_section = section
# 更新菜单项状态
sections = list(self.tools.keys())
current_index = sections.index(section)
# 启用/禁用上移下移菜单项
self.nav_menu.entryconfig("上移", state=tk.NORMAL if current_index > 0 else tk.DISABLED)
self.nav_menu.entryconfig("下移", state=tk.NORMAL if current_index < len(sections) - 1 else tk.DISABLED)
try:
self.nav_menu.tk_popup(event.x_root, event.y_root)
finally:
self.nav_menu.grab_release()
def _move_category_up(self):
"""将当前选中的分类上移"""
if not hasattr(self, "selected_nav_section") or not self.selected_nav_section:
return
sections = list(self.tools.keys())
current_index = sections.index(self.selected_nav_section)
if current_index > 0:
# 交换字典中的顺序
prev_section = sections[current_index - 1]
self.tools = {k: v for k, v in zip(
sections[:current_index - 1] +
[self.selected_nav_section, prev_section] +
sections[current_index + 1:],
[self.tools[k] for k in
sections[:current_index - 1] +
[self.selected_nav_section, prev_section] +
sections[current_index + 1:]]
)}
self._save_config()
self._reload_config()
# 更新选中状态
self.selected_nav_section = self.selected_nav_section
self.selected_nav_button = None # 会在_reload_config中重建
def _move_category_down(self):
"""将当前选中的分类下移"""
if not hasattr(self, "selected_nav_section") or not self.selected_nav_section:
return
sections = list(self.tools.keys())
current_index = sections.index(self.selected_nav_section)
if current_index < len(sections) - 1:
# 交换字典中的顺序
next_section = sections[current_index + 1]
self.tools = {k: v for k, v in zip(
sections[:current_index] +
[next_section, self.selected_nav_section] +
sections[current_index + 2:],
[self.tools[k] for k in
sections[:current_index] +
[next_section, self.selected_nav_section] +
sections[current_index + 2:]]
)}
self._save_config()
self._reload_config()
# 更新选中状态
self.selected_nav_section = self.selected_nav_section
self.selected_nav_button = None
def _setup_navigation_context_menu(self):
"""设置导航栏右键菜单"""
# 使用配置中的菜单字号
menu_font = ("微软雅黑", self.config["菜单字号"])
self.nav_menu = tk.Menu(self.master, tearoff=0, font=menu_font)
for label, command in [
("上移", self._move_category_up),
("下移", self._move_category_down),
None, # 分隔符
("添加分类", self._add_category),
("删除分类", self._delete_category),
("重命名分类", self._rename_category),
]:
if label is None:
self.nav_menu.add_separator()
else:
self.nav_menu.add_command(label=label, command=command)
|
|