Use the command tree.headingas suggested by Mihail above, but note that if you work from class, you will need to pass yourself into the method as soon as possible.
Here is a Python 2 snippet that loads a tree view and demonstrates a call to both a method and an external function:
import Tkinter
import ttk
class TreeWindow:
def __init__(self):
win = Tkinter.Tk()
tree = ttk.Treeview(win,height=10,padding=3)
self.tree = tree
self.win = win
self.tree["columns"] = ("Column 1","Column 2")
self.tree.grid(row=1,column=0,sticky=Tkinter.NSEW)
self.tree.column("Column 1", width=100)
self.tree.heading("Column 1", text="Column 1", command=PrintColumnName1)
self.tree.column("Column 2", width=100)
self.tree.heading("Column 2", text="Column 2", command=self.PrintColumnName2)
self.tree.insert('', 0, text="Row 1", values=("a",1))
self.tree.insert('', 1, text="Row 2", values=("b",2))
self.win.mainloop()
def PrintColumnName2(self):
print("Column 2")
def PrintColumnName1():
print("Column 1")
treeWindow = TreeWindow()
Please note that for some reason, the first click does not seem to work right away, but gets stuck in the buffer until you click a second time - I really want to hear anyone who explains this.
source
share