1

I have an application that has a number of entry widgets and a treeview widget. What I'm trying to do is have a user select a row (child) in the treeview, have certain pieces of data extracted from the child and displayed in differing entry widgets. The user can change or leave whatever data is necessary, hitting 'Return' on each entry widget until the last. Hitting 'Return' on the last entry widget should give focus back to the Treeview, specifically the next child in the list (the row/child immediately below what was originally clicked.

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)

So, this along with the rest of my code (huge, I think I'm showing everything important here, please let me know if more is needed) works in part. a_to_b, b_to_c work correctly. When c_to_nex is called (I know its called when I hit return because of the feedback prints) I know nextID is correct as it does print the correct child ID, but nothing else happens. I know treeview has focus because hitting up or down on the keyboard traverses the rows. I also know that the row nextID describes is 'sort of' in focus because when I hit down, the third row (below the nextID row) is highlighted.

This 'sort of' focus on the nextID row doesnt help me, since I need the row to be selected as if the user clicked on it his or herself.

This

describes a similar question asked a while back, unfortunately, none of those answers helped. I know I'm close, and I know im probably using 'self.dirdisplay.focus(self.nextID)' either incorrectly or with missing options.

Thanks!

Community
  • 1
  • 1
WiC_1989
  • 31
  • 7

1 Answers1

2

After lots of tries, i've finally managed to figure it out!

With self.dirdisplay being a ttk.Treeview widget...

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)

You need the trio above to act as if you are programmatically setting focus to a specific child/row in your Treeview - as if the user clicked on it his or herself.

self.dirdisplay.focus_set() gives the Treeview widget input focus so that pressing the up/down keys works properly.

self.dirdisplay.selection_set((self.nextID, self.nextID)) gives you the effect of the row in question actually being highlighted. self.nextID is repeated because selection_set seems to want a list of items (as per the documentation) opposed to a single row/child id.

Finally, self.dirdisplay.focus(self.nextID) seems to be what actually gives the row focus (any functions you have bound to <> will activate).

WiC_1989
  • 31
  • 7
  • Thank you! Regarding treeview.selection_set(), in using your solution, I did not need to provide a list. A single value, a single value in a one element list, or repeated as you have all work. – fitzl Jan 11 '19 at 00:45