0

Help me please to decide problem with listbox in TCL. I created the next listbox:

listbox .lb1 -height 6 -width 10 -selectmode browse
.lb1 insert 0 "String 1" "String 2" "String 3" "String 4" "String 5" "String  6"

label .label1 -text [.lb1 get active]
button .butt1 -text "enter" -command {.label1 configure -text [.lb1 get active]}
pack .label1 .lb1 .butt1 -expand yes -fill both

How I can change automatically contents of label "label1" without use button "butt1" ?

I want the contents of "label1" will change immediately when I click on one of the list items.

Thanks!

1 Answer 1

3

When you select an item in the listbox, it sends the <<ListboxSelect>> to itself. You can bind to this to react to selection changes:

bind .lb1 <<ListboxSelect>> {.label1 configure -text [.lb1 get active]}

Note that you're also getting very close to the point where using a helper procedure is advised. Even for something simple like this, it makes things easier to write, test and debug.

proc SelectionHappened {listbox label} {
    set activeItem [$listbox get active]
    $label configure -text $activeItem
}
bind .lb1 <<ListboxSelect>> {SelectionHappened .lb1 .label1}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.