How to get the mouse cursor position in TK?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Mon Oct 7 12:03:56 EDT 2002


On Monday 07 Oct 2002 2:58 pm, Heiner Litz wrote:
> HI
>
> i have got a listbox widget. I want to select one line and then push the
> right mouse button to pull up a menu.
>
> I need the mouse position in pixels so I know where to position the menu
> widget and I need to know in what line the right mouse button was pressed,
> because the menu depends on the context of the selected text in the
> listbox.
>
> thx a lot!


Assuming you pass the event to the popup function 
event.x_root and event.y_root should be what you want:

Here is my popup menu maker (can't remember where I stole this one from!!!)

    def makePopUpMenu(self, items, event):
        menu=Menu(None,tearoff=0)
        for item in items:
            if item == 'seperator':
                menu.add_seperator({})
            elif type(item) == ListType:
                for num in item:
                    menu.entryconfig(num, state=DISABLED)
            elif type(item[2]) != ListType:
                menu.add_command(label     = item[0],
                                 underline = item[1],
                                 command   = item[2])
            else:
                submenu = self.addMenuItems(menu, item[2])
                menu.add_cascade(label     = item[0],
                                 underline = item[1],
                                 menu      = submenu)
        menu.tk_popup(event.x_root,event.y_root,0)
        return menu




-- 
### Python Powered Signature
I started writting this email on 
Mon Oct  7 16:01:48 2002





More information about the Python-list mailing list