Events and the Listbox widget

Hans Kristian Ruud hans.kristian.ruud at inenco.no
Thu Oct 5 07:03:23 EDT 2000
>
> >>> Hans Kristian Ruud <hans.kristian.ruud at inenco.no> 10/03/00 09:27AM >>>
> I have generated a listbox, and wish to extract some information when a
> user
>   clicks on an item:
>
>           list = Listbox(canvas,width=15 )
>           list.pack(side=TOP)
>           list.bind('<ButtonPress>', chooseSheet)
>
>           def chooseSheet(event):
>                   listbox = event.widget
>                   selected = listbox.curselection()
>                   setCurSheet( selected[0] )
>
>   My problem is that the list of selected values from
> listbox.curselection()
>   is not updated until after chooseSheet is called, i.e. the function
>
>   listbox.curselection()
>
>   returns the PREVIOUS element that was clicked.
>
>   Originally I had bound the function to root, and then it worked fine,
> however
>   I do not want the function chooseSheet to be executed when I click on
> buttons
>   and such that I have added later.
>
>   Any ideas?
>   Thanks in advance

>
> Jake Baker wrote:
>
>> Use list.bind('<<ListboxSelect>>') instead.
>>
>
> This would of course be the ideal solution; however this feature was only
> implemented in
> Tcl/Tk 8.3.?, whereas Python 1.5.2 comes with Tcl/Tk 8.0. Upgrading to
> Python 1.6 is
> not an option, as the win32com package is not available for Python 1.6
>

This is the solution that I arrived at:

list   = Listbox(canvas, width=15 )
list.bind('<ButtonPress>', self.chooseSheet)
.
.

 def chooseSheet(event):
    listbox = event.widget
    selection = listbox.nearest(event.y)
    setCurSheet( selection )



--
Hans Kristian





More information about the Python-list mailing list