I would like to use a mouse pointer to select a cell in a table created by tkinter.treeview() and print out the cell's value. How can I do this?
Below is a sample code to create a tkinter.treeview table. In this case, I would like to use my mouse pointer to click on one of the values say "Airport". However, when I click that cell, the entire row (i.e. the widget item) is instead selected. How can I avoid this so that I can get what I want?
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Frame):
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.tree = ttk.Treeview(parent, columns=("size", "modified"))
self.tree["columns"] = ("date", "time", "loc")
self.tree.column("date", width=80)
self.tree.column("time", width=80)
self.tree.column("loc", width=100)
self.tree.heading("date", text="Date")
self.tree.heading("time", text="Time")
self.tree.heading("loc", text="Loc")
self.tree.bind('<ButtonRelease-1>', self.selectItem)
self.tree.insert("","end",text = "Name",values = ("Date","Time","Loc"))
self.tree.insert("","end",text = "John",values = ("2017-02-05","11:30:23","Airport"))
self.tree.insert("","end",text = "Betty",values = ("2014-06-25","18:00:00","Orchard Road"))
self.tree.grid()
def selectItem(self, event):
curItem = self.tree.focus()
print (self.tree.item(curItem))
if __name__ == "__main__":
window = tk.Tk()
app = App(window)
window.mainloop()