You set the event handler for the column headings separately from that
for the items.
Here's an example:
--8<----
#!python3.8
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title('Treeview Example')
self.treeview = ttk.Treeview(self, columns=['#1'],
displaycolumns='#all', show=['headings'])
self.treeview.heading('#1', text='Items', command=lambda iid='#1':
self.on_heading(iid))
self.treeview.bind('<Double-1>', self.on_dclick)
self.treeview.pack(fill='both', expand=True)
for i in range(5):
iid = self.treeview.insert('', 'end')
self.treeview.set(iid, '#1', 'Item %d' % (1 + i))
def on_heading(self, iid):
print('Clicked on column %s' % iid, flush=True)
selection = self.treeview.selection()
print('Selection is %s' % ascii(selection), flush=True)
def on_dclick(self, event):
print('Double-clicked on an item', flush=True)
selection = self.treeview.selection()
print('Selection is %s' % ascii(selection), flush=True)
App().mainloop()
--8<----
If you click on the column heading you get:
Clicked on column #1
Selection is ...
If you double-click on, say, item 1, you get:
Double-clicked on an item
Selection is ('I001',)