I've got a bit of code that is suppose to change the text displayed in the entry fields depending on which item is selected in the tree view. I've bound the event to both the left mouse and up and down arrow keys and it's sort of working. Problem is the item in the tree that is currently selected isn't the value being displayed in the entry field. If there are three (5, 7, 4) values and the item with value 4 is selected, the entry field will display the item with value 7 in it. It's always one off. I hate to ask a question about this because I feel like the answer is so simple but I'm really at a loss. Here is my code.
# create GUI items to populate frames
self.create_menu()
self.create_edit_display()
self.albumDisplay = Label(self.editFrame)
self.create_import_display()
self.tree.bind("<BackSpace>", self.delete_insert)
self.tree.bind("<Button-1>", self.update_editFrame)
self.tree.bind("<Up>", self.update_editFrame)
self.tree.bind("<Down>", self.update_editFrame)
# selected track can be removed
def delete_insert(self, event):
self.tree.delete(self.tree.focus())
def update_editFrame(self, event):
trackInfo = self.tree.item(self.tree.focus())
print(trackInfo)
if self.tree.focus():
trackInfo = self.tree.item(self.tree.focus())
self.trackNumberEntry.delete(0, END)
#self.trackNumberEntry.insert(0, random.choice([1,2,3,4,5,6,7]))
self.trackNumberEntry.insert(0, trackInfo['values'][0])
else:
self.tree.focus_get()
Through my debugging I've noticed that every time I first click on the tree view I get something like.
{'tags': '', 'text': '', 'values': '', 'image': '', 'open': 0}
{'tags': '', 'text': 'IMPORT# 2', 'values': [6, 'Supersymmetry', '', 'Arcade Fire', 'Arcade Fire', 'Reflektor', '', '', 2013, '00:11:16', '', '', 'arcade_fire_reflektor_06_supersymmetry_.mp3', 'arcade_fire_reflektor.jpg', ''], 'image': '', 'open': 0}
It's that initial call that's messing up the order. I'm not sure why it's doing that before getting right into 'value''s values.
The simple solution is to bind to <<TreeviewSelect>>
instead of binding to all the ways that the current item can be changed.
The reason your bindings always seem to be one behind is that they are. Custom bindings on widgets fire before the built-in bindings, and it is the built-in bindings which change the selected item. Thus, in your callbacks you are seeing the state of the tree before the selected item is changed.
For a longer explanation see https://stackoverflow.com/a/11542200/7432