Problem with mouse event in listbox 
Author Message
 Problem with mouse event in listbox

Hello,

On both Unix Solaris and Windows 98, Tcl/Tk 8.3.2, I have the following
simple script which is based on an example in the book "Effective Tcl/Tk
Programming."  I source the code into wish.

listbox .lb
pack .lb

foreach i {alpha beta gamma delta epsilon zeta eta theta} {
   .lb insert end $i

Quote:
}

bind .lb <Double-Button-1> {
    puts [.lb get active]

Quote:
}

The program works fine.  But when I change Double-Button-1 to Button-1
(which is what I really want), I get a delayed effect.  Each click on a
new item displays the previous one that was clicked.  It always requires
two clicks (not a double click) to get the desired entry.  This seems
basic to me but I must be missing something since I am new to Tcl/Tk and
the behavior is identical on both OS's.  I have tried numerous changes to
bindtags for the listbox object and class to no avail.  I have also
checked some other books on Tcl/Tk and could not discern an answer.

BTW, I posted this several days ago to the mailing list specifically for
the book mentioned above. But as the only activity on that list has been
my posting it has been of little use.

Any help is most welcome.

Thanks,
Jim



Sun, 16 Mar 2003 03:00:00 GMT  
 Problem with mouse event in listbox

Quote:

> listbox .lb
> pack .lb

> foreach i {alpha beta gamma delta epsilon zeta eta theta} {
>    .lb insert end $i
> }

> bind .lb <Double-Button-1> {
>     puts [.lb get active]
> }

> The program works fine.  But when I change Double-Button-1 to Button-1
> (which is what I really want), I get a delayed effect.  Each click on a

Your confusion stems from the (sometimes hard to follow) interaction
between widget and class bindings, and the bindtags mechanism.  In Tk you
can attach bindings to any arbitrary tag you want, and then attach that
"bindtag" to a particular widget.  This makes it easy to give the same
behavior to a bunch of individual widgets, and in fact this is the
mechanism used to create the class bindings for widgets.  You can query
the current list of bindtags for a widget by using the [bindtags] command:

  % listbox .l
  .l
  % bindtags .l
  .l Listbox . all

The list returned by [bindtags] is ordered, so that bindings attached to a
tag earlier in the list will trigger before bindings attached to a later
tag.  This is ultimately the cause of your particular problem:  your
binding on .l is firing before the Listbox class bindings.

The Listbox class bindings include the following:

  bind Listbox <ButtonRelease-1> {
      tkCancelRepeat

  }

This binding sets the active index for a listbox.  Because of the order of
the bindtags on your widget, the order of events when you click on the
listbox is as follows:

  1.  Your binding on .l queries the current active index
  2.  The Listbox class binding sets the active index to the clicked item

As you can see, the active index is not set until after you have queried
it.

The best way to fix this is to modify your Button-1 binding to specify

index, and specifying the x and y coordinates of the mouse at the time of
the button press.  This form of index means "the item closest to the given
x,y coordinates."  For example:

  bind .lb <Button-1> {

  }

I recommend reading over the listbox manual entry to learn more about
listbox indices, which are powerful and can be pretty useful in a lot of
ways.

   Eric Melski                            The Other Tcl Guy
   ericm at ajubasolutions.com            Ajuba Solutions



Sun, 16 Mar 2003 03:00:00 GMT  
 Problem with mouse event in listbox
In article


Quote:
> Hello,

> On both Unix Solaris and Windows 98, Tcl/Tk 8.3.2, I have the
following
> simple script which is based on an example in the book "Effective
Tcl/Tk
> Programming."  I source the code into wish.

> listbox .lb
> pack .lb

> foreach i {alpha beta gamma delta epsilon zeta eta theta} {
>    .lb insert end $i
> }

> bind .lb <Double-Button-1> {
>     puts [.lb get active]
> }

> The program works fine.  But when I change Double-Button-1 to Button-1
> (which is what I really want), I get a delayed effect.  Each click on
a
> new item displays the previous one that was clicked.  It always
requires
> two clicks (not a double click) to get the desired entry.  This seems
> basic to me but I must be missing something since I am new to Tcl/Tk
and
> the behavior is identical on both OS's.  I have tried numerous changes
to
> bindtags for the listbox object and class to no avail.  I have also
> checked some other books on Tcl/Tk and could not discern an answer.

> BTW, I posted this several days ago to the mailing list specifically
for
> the book mentioned above. But as the only activity on that list has
been
> my posting it has been of little use.

> Any help is most welcome.

> Thanks,
> Jim

Jim,

Did you try and set the selectMode option of the listbox to "single"? I
think that is what you are looking for. The default option here is
"browse".

BTW, for detailed information about Tcl/Tk I highly recommend the book
"Practical Programming in Tcl and Tk" by Mr. Brent Welch. Check pages
359 to 367 (2nd edition) which describes the listbox widget in
magnificent detail. This book is an excellent supplement to the Tcl/Tk
man pages, and compliments "Effective Tcl/Tk Programming" (another
excellent reference).

Hope this helps...

statorvane

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 17 Mar 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem with Single Mouse Click Trapping on Listbox

2. Problem with Right-Mouse-Click in listbox.

3. Problem with Right-Mouse-Click in listbox.

4. BUG: tk4.0b3: listbox selection problem when following mouse

5. Event case missing mouse events on active-x component

6. Problems generating mouse button events in X-Windows

7. problems with "bind"ing mouse event

8. (VA) Having problems with mouse event handling in a ScrolledWindow part

9. Mice, mice, mice, mice ...

10. Mice, mice, mice, mice ...

11. disabling mouse for listbox

12. Q:Listbox select with mouse

 

 
Powered by phpBB® Forum Software