Updated 2013-01-20 08:41:04 by pooryorick

Richard Suchenwirth 2006-04-03 - Tk's listbox widget does not allow disabling items right out of the box. However, with a few lines of code, we can add that capability.

The listbox'disable function can be called on single items to "disable" them (in fact, they only look disabled, because their foreground and background don't change), or, without an item, to remove all those that are disabled from the selection:
package require Tk

proc listbox'disable {w {index ""}} {
    if {$index eq ""} {
        foreach i [$w curselection] {
            if {[$w itemcget $i -selectbackground] eq [$w cget -background]} {
                $w selection clear $i
    } else {
        $w itemconfig $index -selectbackground [$w cget -background] \
                -selectforeground [$w cget -foreground]

#----------------------- Testing demo (displays the selected indices in the titlebar):
set data {Head1 a b c Head2 d e f}
pack [listbox .lb -listvar data -selectmode extended]
foreach i {0 4} {listbox'disable .lb $i}
bind .lb <<ListboxSelect>> {listbox'disable %W; wm title . [%W curselection]}