Tkinter listbox clicking problem
claird at starbase.neosoft.com
Fri Sep 13 22:33:02 CEST 2002
In article <altfrk$1eva$1 at nntp6.u.washington.edu>,
Russell E. Owen <owen at nospam.invalid> wrote:
>In article <sGog9.307183$aA.53945 at sccrnsc02>,
> "Matthew Bonig" <mbonig at hotmail.com> wrote:
>>Right now I have it bound so when a Tkinter listbox gets a <Button-1>
>>(lb.bind("<Button-1>", someFunc)) it calls function someFunc. Now,
>>someFunc() simply prints the curselection(). The problem is it's lagged...
>>if I first select list item 1 (index 0), and then 2, and then 3 (array
>>indexes 0,1,2) it prints out themout incorrectly.
>>(I have someFunc actually do some try, except stuff so I don't get uncaught
>>exceptions) Instead of printing 0,1,2 it prints "nothing selected", 0, 1.
>>The function get's called before the listbox updates it's selection markers!
>>Does anybody know how I could force listbox to update it's selection markers
>>so instead of printing "nothing selected, 0,1 it prints 0,1,2 like it
>I'm hoping somebody has better ideas, but here are some to try:
>Call update_idletasks before you ask for the selection. I have no idea
>if this will work, but it might.
>An ugly solution that would probably work:
>- extract the y coordinate from the event callback
>- call nearest(y_coord) to get the item closest to the mouse click
>- explicitly select that index item. You could try skipping this step,
>but you risk a possible error between what you think they clicked on and
>what is hightlighted. Unfortunately, this step will may be a real pain
>for a list that supports multiple selected items.
>Poll for selection changes -- thus it may take a bit of time before your
>code notices a change, but it will always be right once it gets there.
Whoa! There's a far easier solution. Read up on 'bindtags'
in <URL: http://web.pydoc.org/2.2/Tkinter.html > and elsewhere.
<URL: http://www.cwi.nl/~jacob/textui.py.html > has a small
example that almost is pertinent.
Cameron Laird <Cameron at Lairds.com>
More information about the Python-list