Python Forum

Full Version: Treeview heading event detect.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I try to detect which column of Treeview heading clicked, like below:

from tkinter import Tk, ttk

def click_test(event):
	region = tree_1.identify('region', event.x, event.y)
	if region == 'heading':
		if event.x in range(0, 25):
			print('#0 clicked')
		elif event.x in range(26, 100):
			print('c1 clicked')
		elif event.x in range(101, 175):
			print('c2 clicked')

root_ = Tk()

tree_1 = ttk.Treeview(root_, height = 15, columns = ('c1', 'c2'), selectmode = 'browse')
tree_1.column('#0', width = 25)
tree_1.column('c1', width = 75)
tree_1.column('c2', width = 75)
tree_1.heading('#0', text = '#0', anchor = 'w')
tree_1.heading('c1', text = 'column 1', anchor = 'w')
tree_1.heading('c2', text = 'column 2', anchor = 'w')
tree_1.grid(row = 0, column = 0)

tree_1.bind('<Button-1>', click_test)

root_.mainloop()
Is it have more intuitive/simply way to do that except detect 'event.x' value? similar using '#0', 'c1', 'c2' to identify?
An event must be bound to an event handler.
Here's a good place to learn more: https://tkdocs.com/tutorial/tree.html