How to get the mouse cursor position in TK?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Tue Oct 8 13:33:57 CEST 2002


On Tuesday 08 Oct 2002 10:22 am, Heiner Litz wrote:
> thx again!
>
> BUT its a little bit diffferent: I select many items of the listbox. Then I
> do a right mouse click on one of the selected items. Now I need to know to
> which of the selected items, the right mouse button event belongs to.
>
> thx heiner


Oh ok this is deeper into Tkinter water.....

from pydoc Tkinter.Listbox:-

<snip>

 |  nearest(self, y)
 |      Get index of item which is nearest to y coordinate Y.



So the nearest() method should do what you want.......   however in this case 
I suspect you will need to get the event.y NOT event.y_root and I really 
don't know what will happen if you have a ScrollBar........

lets see.....
Python 2.2.1 (#3, May 29 2002, 20:32:44)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Tkinter import *
>>>
>>>
>>> root=Tk()
>>> lb=Listbox(root)
>>> lb.insert("end", "one")
>>> lb.insert("end", "two")
>>> lb.insert("end", "three")
>>> lb.pack()
>>> def binding(event):
...     print lb.get(first=lb.nearest(event.y))
...
>>> lb.bind("<3>", binding)
'135833868binding'
>>> two
three
two
one
one


I'll leave the ScrollBar issue to you!



Regards
Martin
-- 
### Python Powered Signature
I started writting this email on 
Tue Oct  8 11:28:09 2002





More information about the Python-list mailing list