How to invoke a tkinter menu *itself*

Edward K. Ream edreamleo at charter.net
Fri Feb 10 09:06:44 EST 2006


Hi,



I've spent a pleasant hour or so trying to bring up a top-level Tk menu at 
the same spot as it would appear if I had actually clicked the menu.  That 
is, I want to bring up a menu from the keyboard.



The problem is computing the x and y args to menu.post.  menu.winfo_x and 
menu.winfo_rootx are always 0, presumably be cause the menu isn't packed.



And I haven't been able to use menu.invoke(??) to invoke the menu *itself*. 
One would think this would be a basic Tk functionality.



Any ideas?  Thanks.



Edward



P.S. Here is my best so far. (It must be run from Leo for the Leo magic to 
work.)



import tkFont

name = 'File' # The menu to be invoked.

# A list of all of Leo's menus.

menus = ('File','Edit','Outline','Plugins','Cmds','Window','Help')



# Compute the *approximate* x offsets of each menu.

offsets = {} ; n = 0

for z in menus:

    menu = c.frame.menu.getMenu(z)

    fontName = menu.cget('font')

    font = tkFont.Font(font=fontName)

    offsets[z] = n

    # A total hack: sorta works on windows.

    n += font.measure(z+' '*4)+1



top = c.frame.top # Leo magic.

topx,topy = top.winfo_rootx(),top.winfo_rooty()

menu = c.frame.menu.getMenu(name) # Leo magic.

menu.post(topx+offsets.get(name,0),topy)



EKR
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list