Tkinter: Listboxes, Keybindings?

Fredrik Lundh fredrik at pythonware.com
Sat Mar 29 10:45:33 CET 2003


Jonathan McLin wrote:
> A number of questions, for which I've not found the answer in my Python
> books nor on the web:
>
> Why does the curselection method of an instance of Tkinter.Listbox return a
> single element tuple rather than a scalar?

since multiple items can be selected, if the listbox is configured
to support that.

> Why is this element a string rather than an int?

a flaw in the orginal implementation, which nobody has fixed,
possibly to avoid breaking existing code.  see the "Listbox
patterns" section in "An introduction to Tkinter" for more info:

http://www.pythonware.com/library/tkinter/introduction/listbox.htm
=> patterns

(as you've noticed, the "bug" is still there in 2.2.2)

> Key bindings do not work for me:
>
> For my listbox:
>     ...
>     self.list=Listbox(self)    # self is a subclass of Frame.
>     self.list.bind('<1>',self.ScrollUp)  # works
>     self.list.bind('<Up>',self.ScrollUp)  # doesn't work
>     self.list.bind('<2>',self.ScrollDown) # works
>     self.list.bind('<Down>',self.ScrollDown)  # doesn't work
>     ...
>
> the bindings to <1> and <2> work (calling the bound methods on mouse
> clicks), but the up and down arrow keys on the keyboard do not.  What am I
> doing wrong?

keyboard events are sent to the widget that has the keyboard
focus.  if keyboard bindings don't work as expected, the usual
reason is that some other widget has the focus.

you can use the focus_set() method to explicitly move focus to
a widget (you might want to do this in the Button-1 handler, for
example).

</F>








More information about the Python-list mailing list