Python 3.x 如何在ttk.Treeview中将焦点设置为与单击类似的特定子级?

Python 3.x 如何在ttk.Treeview中将焦点设置为与单击类似的特定子级?,python-3.x,treeview,focus,ttk,Python 3.x,Treeview,Focus,Ttk,我有一个应用程序,它有许多条目小部件和一个treeview小部件。我要做的是让用户在树视图中选择一行(子项),从子项中提取某些数据,并显示在不同的条目小部件中。用户可以更改或保留任何必要的数据,在每个条目小部件上点击“返回”,直到最后一个条目。在最后一个条目小部件上点击“Return”应该会将焦点返回到树状视图,特别是列表中的下一个子项(紧靠最初单击的内容下方的行/子项) def display_chosen(self, event):#Called when user clicks row/c

我有一个应用程序,它有许多条目小部件和一个treeview小部件。我要做的是让用户在树视图中选择一行(子项),从子项中提取某些数据,并显示在不同的条目小部件中。用户可以更改或保留任何必要的数据,在每个条目小部件上点击“返回”,直到最后一个条目。在最后一个条目小部件上点击“Return”应该会将焦点返回到树状视图,特别是列表中的下一个子项(紧靠最初单击的内容下方的行/子项)

def display_chosen(self, event):#Called when user clicks row/child in 'dirdisplay' Treeview widget
        clickedfile = self.dirdisplay.focus()#Get child (row) id of click
        self.nextID = self.dirdisplay.next(clickedfile)#Get next child, place in class-wide accessible variable
        self.enableentries()#Enable entry boxes so data extraction to entry widgets work
        #Do whatever to send data to entry widgets

def enableentries(self):#Sets three entry widgets a, b, c to state normal.
    try:
        self.a.config(state=NORMAL)
        self.b.config(state=NORMAL)
        self.c.config(state=NORMAL)
    except AttributeError:
        pass   
def a_to_b(self, event):#While in entry 'a', hitting 'Return' calls this and sets focus to next entry widget, 'b'
    #Do whatever with data in a
    self.b.focus_set()

def b_to_c(self, event):#While in entry 'b', hitting 'Return' calls this and sets focus to next entry widget, 'c'
    #Do whatever with data in b
    self.c.focus_set()

def c_to_nex(self, event):#Hitting 'Return' on 'c' calls this, setting focus back to 'dirdisplay' treeview, giving focus to child immediately below what was originally clicked.
    #Do whatever with data in c
    print('Focus acknowledged')
    print(self.nextID)#Feedback to show me the nextID actually exists when I hit 'Return'
    self.dirdisplay.focus_set()
    self.dirdisplay.focus(self.nextID)
所以,这和我其余的代码(庞大,我想我在这里展示了所有重要的东西,如果需要更多,请让我知道)部分工作正常。a_to_b,b_to_c工作正常。当调用c_to_nex时(由于反馈打印,当我点击return时我知道它被调用)我知道nextID是正确的,因为它确实打印了正确的子ID,但没有其他事情发生。我知道treeview具有焦点,因为在键盘上向上或向下键会遍历行。我还知道nextID描述的行“有点”处于焦点,因为当我向下键时,第三行(nextID行下方)会高亮显示

这种对nextID行的“某种”关注对我没有帮助,因为我需要选择该行,就像用户自己点击它一样

描述了前一段时间提出的一个类似问题,不幸的是,这些答案都没有帮助。我知道我很接近,并且我知道我可能使用了“self.dirdisplay.focus(self.nextID)”,要么使用错误,要么缺少选项


谢谢!

经过多次尝试,我终于找到了答案

self.dirdisplay是一个ttk.Treeview小部件

def c_to_nex(self, event):#Hitting 'Return' on 'c' calls this, setting focus back to 'dirdisplay' treeview, giving focus to child immediately below what was originally clicked.
    #Do whatever with data in c
    print('Focus acknowledged')
    print(self.nextID)#Feedback to show me the nextID actually exists when I hit 'Return'
    self.dirdisplay.focus_set()
    self.dirdisplay.selection_set((self.nextID, self.nextID))
    self.dirdisplay.focus(self.nextID)
您需要使用上面的三个选项,就好像您正在以编程方式将焦点设置为树视图中的特定子/行一样—就好像用户自己单击了它一样

self.dirdisplay.focus\u set()为Treeview小部件提供输入焦点,以便按向上/向下键可以正常工作

self.dirdisplay.selection_set((self.nextID,self.nextID))提供实际高亮显示相关行的效果。self.nextID会重复,因为selection_set似乎想要一个项目列表(根据文档),而不是一个行/子id


最后,self.dirdisplay.focus(self.nextID)似乎是实际提供行焦点的工具(您绑定的任何函数都将激活)。

谢谢!关于treeview.selection_set(),在使用您的解决方案时,我不需要提供列表。一个值,一个元素列表中的一个值,或者在所有工作完成后重复。