Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tkinter treeview widget - bind doubleclick to items only ?

3,882 views
Skip to first unread message

R.Wieser

unread,
Jan 20, 2020, 5:50:50 AM1/20/20
to
Hello all,

I've create a treeview, and would (ofcourse) want to register some
mouse-clicking on it, and for that I've found the ".bind()" method.

https://stackoverflow.com/questions/3794268/command-for-clicking-on-the-items-of-a-tkinter-treeview-widget

There is a slight problem with it though: when I click a columns caption
(for sorting) the events code also gets fired, which causes problems.

I've found that I can connect mouse-click events to the column captions as
shown in the second example here in the "self.tree.heading()" call :

https://stackoverflow.com/questions/5286093/display-listbox-with-columns-using-tkinter

I was wondering if something similar is maybe also available for the items
below it ...

If not, how do I ignore mouseclicks that are not done on one of the items ?

Regards,
Rudy Wieser


MRAB

unread,
Jan 20, 2020, 3:21:10 PM1/20/20
to
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',)

R.Wieser

unread,
Jan 21, 2020, 1:31:02 AM1/21/20
to
MRAB,

> self.treeview.bind('<Double-1>', self.on_dclick)

That is what I used. Doubleclicking on the heading caused the "on_dclick"
code to execute, though initially the ".identify()" call in my code returns
an empty ID (no row present at the location I clicked).

That changes when the treeview contents are scrolled up, causing a with a
row to be hidden behind the heading. At that moment doubleclicking the
heading my ".identify()" call in the "on_dclick" code returns the ID of the
row thats hidden behind it. :-(

Shucks, it seems to be even worse:
I just scrolled a row containing children (visible by its prefixed the
to-the-right pointing triangle) behind the heading, and double-clicking the
heading caused that rows children to unfold and fold.

I don't think that that should be happening ... How do I prevent it ?

Regards,
Rudy Wieser


MRAB

unread,
Jan 21, 2020, 1:54:29 PM1/21/20
to
Huh, I never noticed that!

I think it's an issue with Tk itself. tkinter ("Tk interface") is a
wrapper around Tcl/Tk, Tk being the GUI extension for the Tcl
programming language.

R.Wieser

unread,
Jan 21, 2020, 3:37:20 PM1/21/20
to
MRAB,

> Huh, I never noticed that!

Yep. And as that unfolding and folding is fully done inside the treeview
widget itself there is no way you can fix it with a bit of python code. :-(

> I think it's an issue with Tk itself.

Most likely. Rather odd that that issue it still exists though. I can't
imagine I'm the first one who noticed this "bleeding thru" (the heading)
effect.

But ... Any way how I can tell the doubleclick code to ignore clicks
outside of the treeview-item window* (as in: excluding the header) ? That
would at least be something.

*like any way to retrieve the relative cooridinates of the rows subwindow ?
Or the relative coordinates of the heading bar ? I've not found anything
in that regard.

Regards,
Rudy Wieser


R.Wieser

unread,
Jan 25, 2020, 12:29:07 PM1/25/20
to
"R.Wieser" <add...@not.available> wrote in message
news:r040ls$fje$1...@gioia.aioe.org...
0 new messages