[Tkinter-discuss] [Tkinter]Right click popup

Russell E. Owen rowen at cesmail.net
Tue Jul 29 21:08:07 CEST 2008


In article <18686679.post at talk.nabble.com>,
 Ambigioz <pbanfis at hotmail.it> wrote:

> Hello,
> I'm trying to do a notepad with tkinter in windows (well also for linux),and
> i'd like to create a popup with 'copy' , 'paste', 'cut', bla bla,on right
> click.
> My only problem it's to create that popup,because it's not neither toplevel
> nor alerts dialogs...can you help me?:p

You just bind a callback to the right-click and use it to post a Menu at 
the appropriate location, which you can get from the event's x_root and 
y_root fields.

My RO python package includes RO.Wdg.CtxMenu which will add a contextual 
menu to any widget.
<http://www.astro.washington.edu/rowen/ROPackage/Overview.html>
It is available from PyPI. RO.Wdg also includes a set of widgets that 
support contextual menus and "hot help".

In that package I bind to "<<CtxMenu>>" and then use another bit of code 
to send the <<CtxMenu>> event in a platform-independent way. When the 
event fires the following occurs:
    def __postMenu(self, evt):
        """Posts the contextual menu"""
        menu = self.ctxGetMenu()
        if menu.index("end") != None:
            menu.tk_popup(evt.x_root, evt.y_root)

-- Russell



More information about the Tkinter-discuss mailing list