Skip to content Skip to sidebar Skip to footer

Selecting Data From A Treeview

I use a ttk.TreeView as a multicolumn ListBox which effectively displays the sql data I send to it as a table. When I make a sql query and the treeview displays the queried data, t

Solution 1:

first you need to bind a event to your tree iv used double click

self.tree.bind("<Double-1>",lambda event :self.OnDoubleClick(event))
#note the OnDoubleClick is the name of the sub that python will look for when tree #double clicked

next you need to make your subroutine that will be called when tree double click (for my example it is OnDoubleClick)

defOnDoubleClick(self, event):
        frame3 = tk.LabelFrame(self, text="frame1", width=300, height=130, bd=5)
        frame3.grid(row=2, column=0, columnspan=3, padx=8)
        #frame is just for managing objects not absolutely needed but i think it is good#to use frame when using object so i have kept it in
        curItem = self.tree.focus()
        contents =(self.tree.item(curItem))
        selectedetails = contents['values'] 
        #this is what you would use to when presenting the selectedd information

Then to access this selected data just use the name of the array with the indentation that you want(in this example selectedetails). I then use string variables to fill in entrys with the selected data.

self.example_var = StringVar()
self.example_var.set(selectedetails[1])
self.example_txt = Entry(frame3,textvariable=self.example_var)
self.example_txt.grid(row=1,column=1)

Post a Comment for "Selecting Data From A Treeview"