使用绑定键将焦点和选择从 tk 条目移动到 ttk 树视图

Move focus and selection from tk entry to ttk treeview with binding key

我想使用绑定键将焦点和选择从 tk 条目更改为 ttk 树视图部分。

上图,当前光标和焦点在入口部分,想将焦点和选择移动到 ttk 树视图部分

    def treeview_focus(self, event):

    child_id = self.tree.get_children()[-1]
    
    self.tree.focus(child_id)
    self.tree.selection_set(child_id)
    self.tree.selection_add(child_id)

使用焦点和选择方法,我可以在树视图中突出显示特定项目列表,但我无法通过键盘上的上下键移动项目列表,因为光标仍在条目中。

如果我用鼠标直接点击树视图中的项目列表,我可以用键盘上下移动列表。

我的问题是如何在不单击鼠标的情况下使用我的键盘选择树视图部分中的项目。

我发现问题所在了。

应该首先使用 focus_set() 方法 select 获取和聚焦整个树视图

self.tree.focus_set()
self.tree.focus(child_id)

以上序列,我可以select树视图的最后一项,并且可以用我的键盘移动列表