Reputation: 117
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, there is the option to select the data, as in the row is highlighted when clicked. Is it possible for me to click a row so that data is highlighted and then click another button that creates a popup window with the data in it to be edited?
Because I am working with SQL, I just need the data to be selected and then I can use it to delete it from the SQL table, not the treeview table. Here is the tree below with an example of some data being selected. Can I pass only the selected data to be edited or deleted or something to that effect?
Edit:
def OnDoubleClick(self,event):
top1=Toplevel(height=600,width=500)
#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
self.example_var = StringVar()
self.example_var.set(selectedetails[1])
self.example_txt = Entry(top1,textvariable=self.example_var)
self.example.grid(row=1,column=1)
I changed the frame for managing objects into a Toplevel and changed where the the entry widget goes so that its the same place (top1). The error message that is produced when I double click on an item in the tree is:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1489, in __call__
return self.func(*args)
File "C:\Users\lukeh\Documents\a\test for double click.py", line 278, in OnDoubleClick
self.example.grid(row=1,column=1)
AttributeError: 'MultiColumnListbox' object has no attribute 'example'
When I delete the second half of the code where the self.example starts, the code doesn't actually seem to do anything other than create the Toplevel.
Edit:
When I delete the self.example lines of code and just use print (selectedetails)
the correct line of data is outputted.
Upvotes: 2
Views: 2567
Reputation: 229
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)
def OnDoubleClick(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)
Upvotes: 3