如何关注树视图孩子

问题描述 投票:0回答:1

我的表有父行和子行 当我选择子行并删除子项时 之后表格切换并聚焦于父行

我的问题是如何专注于儿童排

备注: 我试过这样

children = 树.getchildren() 表.焦点(子项)

反正没效果

python tkinter treeview children
1个回答
0
投票

要在删除另一个子行后保持对树结构中子行的焦点,您需要确保正确识别所需的子行并将焦点设置在所需的子行上。您使用的方法似乎是在正确的轨道上,但可能需要一些调整才能正常工作。您可以遵循以下一般方法:

识别当前选择:在删除子行之前,识别当前选择的是哪一行。此信息稍后将用于重置焦点。

删除目标子行:继续删除目标子行。

将焦点设置回特定行:删除后,将焦点设置回表中仍然存在的子行。如果删除了先前选择的行,您可能需要决定要关注的新行(例如下一个或上一个同级行,或父行)。

这是一个伪代码示例:

# Get the current selection
current_selection = tree.get_selected_row()

# Delete a child row
tree.delete_row(some_row_id)

# After deletion, check if the current selection still exists
if current_selection in tree.get_all_rows():
    # If yes, set the focus back to it
    tree.set_focus(current_selection)
else:
    # If not, choose a new row to focus
    # For example, focus on the next sibling, previous sibling, or parent row
    new_focus_row = determine_new_focus_row(current_selection)
    tree.set_focus(new_focus_row)
© www.soinside.com 2019 - 2024. All rights reserved.