Tk Source Code

View Ticket
Login
Ticket UUID: 7aae3d0fe508fb7c6d88553318fbd84111f39d47
Title: ttk::treeview's move command does nothing when moving down in Tk 9.1
Type: Bug Version: 9.1
Submitter: m.n.summerfield Created on: 2026-10-05 12:30:43
Subsystem: 88. Themed Tk Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-10-10 20:01:42
Resolution: Fixed Closed By: bohagan
    Closed on: 2026-10-10 20:01:42
Description:

If you run the following program in 9.0.3 it works fine. You can press the Up and Down buttons and the selected item will move up and down as expected. But in 9.1.0 pressing Down doesn't work; also the focus looks odd in 9.1.0 compared with 9.0.3.

# tcl
proc main {} {
    tk scaling 1.5
    wm minsize . 240 320
    ttk::button .up -text Up -underline 0 -command on_up
    ttk::button .down -text Down -underline 0 -command on_down
    ttk::treeview .tree
    foreach letter [split ABCEDF ""] {
        .tree insert {} end -id $letter -text $letter
    }
    .tree insert {} end -id moveme -text "Move Me"
    foreach letter [split GHIJKLM ""] {
        .tree insert {} end -id $letter -text $letter
    }
    .tree selection set moveme
    grid .up -row 0 -column 0 -sticky w
    grid .down -row 0 -column 1 -sticky e
    grid .tree -row 1 -column 0 -columnspan 2 -sticky news
    grid rowconfigure . 1 -weight 1
    grid columnconfigure . 0 -weight 1
    bind . <Escape> exit
    bind . <Control-q> exit
    bind . <Alt-u> on_up
    bind . <Alt-d> on_down
}
proc on_up {} {
    if {[set iid [lindex [.tree selection] 0]] ne ""} {
        if {[set prev [.tree prev $iid]] ne ""} {
            .tree move $iid {} [.tree index $prev]
            puts "move $iid up"
        }
    }
}
proc on_down {} {
    if {[set iid [lindex [.tree selection] 0]] ne ""} {
        if {[set next [.tree next $iid]] ne ""} {
            .tree move $iid {} [.tree index $next]
            puts "move $iid down"
        }
    }
}
main
User Comments: bohagan added on 2026-10-10 20:01:42:
No response from initiator. Closing as fixed.

bohagan added on 2026-10-06 03:35:33:
I found the issue. The logic did not account for the need to go to the next item when the move to index is after the item's current index. See branch treeview_move_bug_7aae3d0fe5 and commit [ed9a60d0]. I tested it on 8.6, 9.0, and 9.1. Let me know if you see any further issues.

If you want something that will work now and after the fix use the following:

Add this before you set the selection...

".tree focus moveme"

... and replace on_up and on_down with:


if {$::tk_version < 9.1} {
proc on_up {} {
    if {[set id [.tree focus]] eq ""} {
	set id [lindex [.tree children {}] 0]
    }
    if {$id ne ""} {
        if {[set prev [.tree prev $id]] ne ""} {
            .tree move $id {} [.tree index $prev]
            puts "move $id up"
        }
    }
}
proc on_down {} {
    if {[set id [.tree focus]] eq ""} {
	set id [lindex [.tree children {}] 0]
    }
    if {$id ne ""} {
        if {[set next [.tree next $id]] ne ""} {
            .tree move $id {} [.tree index $next]
            puts "move $id down"
        }
    }
}
} else {
proc on_up {} {
    if {[set id [.tree focus]] eq ""} {
	set id [.tree id {} 0]
    }
    if {$id ne ""} {
        if {[set prev [.tree before $id]] ne ""} {
            .tree move $id before $prev
            puts "move $id up"
        }
    }
}
proc on_down {} {
    if {[set id [.tree focus]] eq ""} {
	set id [.tree id {} 0]
    }
    if {$id ne ""} {
	if {[set next [.tree after $id]] ne ""} {
            .tree move $id after $next
            puts "move $id down"
        }
    }
}
}