Tk Listbox bindings (was: Tk Listbox Question) 
Author Message
 Tk Listbox bindings (was: Tk Listbox Question)


Quote:
> It's actually pretty simple... most of what you need can be found in
> Fred's "Introduction to Tkinter" at:
> http://www.*-*-*.com/

> Here's a {*filter*} though.  It's in a class wrapper because I've build
> myself a 'standard' set of compound widgits that I use.  You could call
> this by simply calling:  "list = display_list(parent, items_in_list)"

> #######################################################################
> class display_list(Frame):

> [snip]

>         #Here's the bindings

>         self.listbox.bind('<Double-Button-1>', self.do_this)
>         self.listbox.bind('<Control-Button-1>', self.do_that)
>         self.listbox.bind('<Button-2>', self.do_something_else)

While it's easy to bind mouse events to a listbox, I haven't managed
to bind keyboard events to it. In a current project I have a listbox
and I want my users to be able to scroll through it using common keys
like <Up> and <Down>. However, binding them to the listbox, as done
above, does not work; I don't know why.

If I do a bind_all, it does work, though. Unfortunately, this is not
the right way to do it; I have another window with a ScrolledText
widget, and if I scroll there, the listbox scrolls too, due to the
bind_all effect!

Experimenting, I also tried binding "<Down>" (etc) to all widgets in
the current frame, which doesn't work either.

So my obvious question is, how can I bind keyboard events to a
listbox without having to use bind_all?

TIA,


Homepage: http://www.*-*-*.com/
You call me a masterless man. You are wrong. I am my own master.



Mon, 27 May 2002 03:00:00 GMT  
 Tk Listbox bindings (was: Tk Listbox Question)

Quote:

> While it's easy to bind mouse events to a listbox, I haven't managed
> to bind keyboard events to it. In a current project I have a listbox
> and I want my users to be able to scroll through it using common keys
> like <Up> and <Down>. However, binding them to the listbox, as done
> above, does not work; I don't know why.

> If I do a bind_all, it does work, though. Unfortunately, this is not
> the right way to do it; I have another window with a ScrolledText
> widget, and if I scroll there, the listbox scrolls too, due to the
> bind_all effect!

> Experimenting, I also tried binding "<Down>" (etc) to all widgets in
> the current frame, which doesn't work either.

> So my obvious question is, how can I bind keyboard events to a
> listbox without having to use bind_all?

The problem is that key press/release events are sent to the widget that
currently has input focus, which by default is the toplevel window.  To
explicitly set focus to the listbox when clicked, use something like the
following:

class display_list(Frame):
    def __init__(self, parent, items, height = 15, width = 20):
        # ... initialization snipped ...
        self.listbox.bind('<ButtonRelease-1>', self.setfocus)

    def setfocus(self, ev):
        self.listbox.focus()

Incidentally, the <Up> and <Down> behavior you described is already in
Tk's default listbox bindings (see listbox.tcl), so you should be done
once you set focus.

Regards,
Stephen

--



Tue, 28 May 2002 03:00:00 GMT  
 Tk Listbox bindings (was: Tk Listbox Question)


Quote:
>>         #Here's the bindings

>>         self.listbox.bind('<Double-Button-1>', self.do_this)
>>         self.listbox.bind('<Control-Button-1>', self.do_that)
>>         self.listbox.bind('<Button-2>', self.do_something_else)
>While it's easy to bind mouse events to a listbox, I haven't managed
>to bind keyboard events to it. In a current project I have a listbox
>and I want my users to be able to scroll through it using common keys
>like <Up> and <Down>. However, binding them to the listbox, as done
>above, does not work; I don't know why.

May be because your listbox doesn't got the keyboard focus?
You can move around the keyboard focus using the TAB key or
otherwise you can set the keyboard focus using the 'focus_set()'
method from within your program.

Hope this helps.

Regards, Peter
--
Peter Funk, Oldenburger Str.86, D-27777 Ganderkesee, Germany, Fax:+49 4222950260
echo '[dO%O+38%O+PO/d0<0]Fi22os0CC4BA64E418CE7l0xAP'|dc



Thu, 30 May 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Making Tk listboxes behave like MS-Windows listboxes

2. Tk 4.1b3 bug report ( default binding for listbox )

3. Tk Listbox Question

4. TK listbox question

5. tcl/tk listbox implementation question

6. newbie question on Tk listboxes

7. tk newbie question: listbox with non-contiguous selections

8. Listbox widget binding question

9. Grail and Tk Listboxes

10. ListBoxes [Tk]

11. Need assistance with TK listboxes/dropboxes

12. Listbox position (TK)

 

 
Powered by phpBB® Forum Software