Listbox.. ACTIVE-item is the one selected previously..

Stephen J. Turner sjturner at ix.netcom.com
Mon Nov 1 12:36:31 EST 1999


Andrew Markebo wrote:
> I want to have a listbox, and when the user clicks on one item in it,
> he gets the value selected 'published' somewhere.. with the small
> program included below.. If it is launched, and the user selects the
> letters in order I would assume the result to be "abcde", but what I
> get is: "aabcd", after the first selection, ACTIVE is lagged..
> 
> --------------------
> 
> z=None
> def click(event=None):
>    global z
>    print z.get(ACTIVE)
>    
> def test():
>    global z
>    frame=Frame(None)
>    frame.pack()
>    z=Listbox(frame)
>    z.pack()
>    z.bind("<Button-1>", click)
>    for i in ("a", "b", "c", "d", "e"):
>       z.insert(END, i)
> 
>    frame.mainloop()
> 
> test()

The code to activate the clicked element is found in the default listbox
class bindings (see listbox.tcl in your Tk distribution).  This requires
two changes to your script:

1.  The <Button-1> event specification should be changed to
    <ButtonRelease-1>, since activation occurs on button release.
2.  Your binding must be called after the class binding.

The second can be accomplished in two ways.  The first is to modify the
bindtags list for your listbox widget so that _all_ widget bindings
occur after class bindings:

   z.bind("<ButtonRelease-1>", click)
   tags=list(z.bindtags())
   mytag=tags[0]
   del tags[0]
   tags.insert(1, mytag)
   z.bindtags(tuple(tags))

This could cause problems, though, if you have other widget bindings
that you want to precede listbox class bindings.  In this case, what you
must do is associate your function with a new bindtag (e.g.,
"postclass"), and then insert the new bindtag in the listbox widget's
bindtags list after the class binding:

   z.bind_class("postclass", "<ButtonRelease-1>", click)
   tags=list(z.bindtags())
   tags.insert(2, "postclass")
   z.bindtags(tuple(tags))

Regards,
Stephen

--
Stephen J. Turner <sjturner at ix.netcom.com>




More information about the Python-list mailing list