Tk Source Code

View Ticket
Login
Ticket UUID: 047b2297a74b1d282835094b3b293e9e374de730
Title: ttk.Treeview .selection() giving incorrect result
Type: Bug Version: 8.6
Submitter: anonymous Created on: 2026-02-04 13:31:57
Subsystem: 53. [selection] Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2026-02-08 02:04:00
Resolution: None Closed By: nobody
    Closed on:
Description:

I noticed a problem regarding selection state in the ttk.Treeview.

I already described the issue here: https://github.com/python/cpython/issues/144439

People there have told me to report it here. Since I am not fluent in TCL, I don't have a minimal, self-contained TCL code example. What I can do though is to link to my project, where I am using tkinter, which supposedly only is a thin wrapper around TclTK: https://codeberg.org/ZelphirKaltstahl/xiaolong-dictionary/src/commit/3ca9c5d100f054b659fbbe69da08f7ff16f73a13/src/lib/custom_widgets/treeview.py#L214.

Here is my description, in case the above link dies:

Bug report

Bug description:

My environment is the following:

  • I am using Python 3.13.5
  • on Debian GNU/Linux 13 (trixie)
  • (desktop environment: KDE)
  • tk.TkVersion: 8.6
  • ttk.tkinter.TkVersion: 8.6
  • tk system package:
    $ apt-cache policy tk
    tk:
     Installed: (none)
     Candidate: 8.6.16
     Version table:
        8.6.16 500
           500 http://deb.debian.org/debian trixie/main amd64 Packages
    
  • apt list --installed | grep tk:
    ...
    python3.13-tk/stable,now 3.13.5-2 amd64 [installed,automatic]
    ...
    

What I have observed is, that there seems to be a bug in the method treeview.selection(). Steps to reproduce:

  1. register handlers for "<<TraverseIn>>", something along the lines of:
    def __init__(self ...):
        self.treeview.bind(
            "<<TraverseIn>>",
            self._on_traverse_in_treeview
        )
    ...
    def _on_traverse_in_treeview(self, event: tk.Event) -> str | None:
        logger.info(self.treeview.selection())
  1. start the application
  2. select one item in the treeview using mouse left click
  3. deselect the only one selected item in the treeview, using ctrl + left click
  4. tab through the widgets, until you focus the treeview again
  5. observe how tkinter still logs the deselected item as selected

Workaround

I have found a workaround, which I am currently using.

        # Binding for working around treeview selection bug.
        self.treeview.bind(
            EventName.TRAVERSE_OUT.value,
            self._on_traverse_out_treeview
        )
    ...
    def _on_traverse_out_treeview(self, event: tk.Event) -> None:
        if len(self.treeview.selection()) == 0:
            self.treeview.selection_clear()
            self.treeview.focus("")

This forces the selection and focus to clear, if no item is selected when tabbing out. Then, when tabbing and reaching the treeview again, if there is no visually selected item, the selection state will have been cleared and .selection() will give the correct result.

Side-effects

  • Not yet observed. I only found this workaround today.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

User Comments: bohagan added on 2026-02-08 02:04:00:
I think I understand what you are doing, and tested it with the following script on Tcl/Tk 8.6.14:

  package require Tk
  grid [ttk::treeview .tv]
  grid [entry .e]
  focus .tv

  # Insert items
  .tv insert {} end -text One
  .tv insert {} end -text Two
  .tv insert {} end -text Three
  .tv insert {} end -text Four
  .tv insert {} end -text Five
  update
  puts [format "1:Widget focus=%s, Treeview item focus=%s, Treeview selection=%s" [focus] [.tv focus] [.tv selection]]

  # Select item
  .tv focus I004
  .tv selection set I004
  update
  puts [format "2:Widget focus=%s, Treeview item focus=%s, Treeview selection=%s" [focus] [.tv focus] [.tv selection]]

  # Clear selection
  .tv selection set {}
  update
  puts [format "3:Widget focus=%s, Treeview item focus=%s, Treeview selection=%s" [focus] [.tv focus] [.tv selection]]

  # Move focus to next widget (this is what tab calls)
  focus [tk_focusNext .tv]
  update
  puts [format "4:Widget focus=%s, Treeview item focus=%s, Treeview selection=%s" [focus] [.tv focus] [.tv selection]]

  # Move focus back to treeview widget
  focus [tk_focusNext .e]
  update
  puts [format "5:Widget focus=%s, Treeview item focus=%s, Treeview selection=%s" [focus] [.tv focus] [.tv selection]]



and got the following results:

1:Widget focus=.tv, Treeview item focus=, Treeview selection=
2:Widget focus=.tv, Treeview item focus=I004, Treeview selection=I004
3:Widget focus=.tv, Treeview item focus=I004, Treeview selection=
4:Widget focus=.e, Treeview item focus=I004, Treeview selection=
5:Widget focus=.tv, Treeview item focus=I004, Treeview selection=


Which shows it works as expected. A suggestion is you may get better results by using the <FocusIn> and <FocusOut> events instead of <<TraverseIn>> and <<TraverseOut>>.

oehhar added on 2026-02-04 15:47:49:

Thanks for your post. The enxt step will be a complete Tcl script, which will show the issue. Your example is basically only a tk bind command, but all the things around are missing (creation of the widget, filling in data). As the ticket is about selection: specially the used selection mode is important. See "-selectmode" in https://www.tcl-lang.org/man/tcl8.6/TkCmd/ttk_treeview.htm

On the practical side, I don't understand the issue.

What should happen:

  • select one item in the treeview using mouse `left click`: an "item" is probably a cell.
  • deselect the only one selected item in the treeview, using `ctrl` + `left click` : ok, does that "deselect" or does it set an anchor for multi-cell selection?
  • tab through the widgets - ok, you have *multiple* widgets, not just a tree widget. Tabbing is probably by using the "Tab" key?
  • now, it is still selected. Which action should deselect it, ctrl+left click or tab?

Thanks again, Harald