Search code examples
pythontreeviewttk

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


I want to change focus and selection from tk entry to ttk treeview section with binding key.

enter image description here

Above figure, present cursor and focus in entry section and want to move focus and selection to ttk treeview section

    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)

with focus, selection method, I can highlight specific list of item in treeview but I can't move item list by up and down key on keyboard because cursor still in entry.

If I click directly list of items in treeview with mouse, I can move up and down of lists with my keyboard.

My question is how can I focus treeview section and can use my keyboard for selecting items in treeview section without mouse clicking.


Solution

  • I found out what is problem.

    should use focus_set() method first for selecting and focusing whole treeview

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

    above sequence, I can select last items of treeview and can move lists with my keyboard