| 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 reportBug description:My environment is the following:
What I have observed is, that there seems to be a bug in the method
WorkaroundI have found a workaround, which I am currently using.
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 Side-effects
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:
Thanks again, Harald | |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki