ActiveState Community

Tk Listbox Up/Down Arrow Key Binding

Posted by dandan on 2008-07-05 12:42

Hello,
Just wondering if someone can give me a small hint on how to bind the up/down arrow keys successfully to a Tk Listbox.

So far I have:

### Down Arrow Binding
bind .top.lis33 {
set i [.top.lis33 activate [expr [.top.lis33 curselection]+1]]
.top.lis33 itemconfigure active -selectbackground blue -selectforeground white
}

### Up Arrow Binding
bind .top.lis33 {
set i [.top.lis33 activate [expr [.top.lis33 curselection]-1]]
.top.lis33 itemconfigure active -selectbackground blue -selectforeground white
}

Am I anywhere close to such a solution for these bindings?

Help much appreciated.

jeffh | Mon, 2008-07-07 13:33

The arrow keys and several other key bindings already exist on the Listbox. However, the widget must have focus for it to receive key bindings, and focus isn't given to a listbox by default when clicking on it. You can force focus in an instance with [focus], or do it for all listboxes with:

bind Listbox {+ focus %W;}