rlght click menu missing in most Tk apps

eltronic at juno.com eltronic at juno.com
Sat Apr 3 17:53:09 EST 2004


On Sat, 03 Apr 2004 09:13:00 -0700 Stewart Midwinter <stewart
midtoad.homelinux.org> writes:
> On Wed, 31 Mar 2004 22:09:57 -0500, eltronic wrote:
> 
> > here is some code to add right click context menus.
> > in the few apps I've edited sofar, it just works, once
> > you run rCbinder with the root of the gui or bind B3
> > all present and future widgets will have right click.
> 
> How about a little example app showing your idea in use?
ok, I kinda did, but heres another.

redemo in yourpythondir/Tools/Scripts/redemo.py
often recommended along with kudos.
good for your sanity when working on regex's

has an entry widget to enter a regular expression,
a text box to enter data and another to show matches.
we are just going to add right click menu to redemo.
import it into your favorite editor.

scroll down to def main(): 
and unfold it if using a folding editor.
add rClickbinder(root) so it looks like:

    rClickbinder(root)
    root.mainloop()

back to the top just after the
import re

insert these 2 functions

def rClicker(e):
    ''' right click context menu for all Tk Entry and Text widgets
    '''
    import Tkinter as Tk

    def rClick_Copy(e, apnd=0):
        e.widget.event_generate('<Control-c>')

    def rClick_Cut(e):
        e.widget.event_generate('<Control-x>')

    def rClick_Paste(e): 
        e.widget.event_generate('<Control-v>')

    def rClick_insert(s,e): #insert s at the current curser position
        e.widget.insert("insert", s)

    e.widget.focus()

    nclst=[
        ('      ',None),   #
        (' Cut', lambda e=e: rClick_Cut(e)),  
        (' Copy', lambda e=e: rClick_Copy(e)),
        (' Paste', lambda e=e: rClick_Paste(e)),

        ('>>>>>>>','insertS'),  #start named cascade  submenu
        (' \\\\n ', lambda s='\\n',  e=e:  rClick_insert(s, e) ),
        ('   [] ', lambda s='[]',  e=e:  rClick_insert(s, e) ),
        ('   () ', lambda s='()',  e=e:  rClick_insert(s, e) ),
        ('>>>>>>>',None),    #end named cascade  submenu

        ]
    
    rmenu = Tk.Menu(None, tearoff=0, takefocus=0)
    cas = {}
    cascade = 0
    
    for (txt, cmd) in nclst:
        if txt.startswith('>>>>>>') or cascade:
            if txt.startswith('>>>>>>') and cascade and cmd == None:
                #done cascade
                cascade = 0
                rmenu.add_cascade( label=icmd, menu = cas[icmd] )
    
            elif cascade:
                if txt == ' ------ ':
                    cas[icmd].add_separator()
                    
                else: cas[icmd].add_command(label=txt, command=cmd)
    
            else:  # start cascade
                cascade = 1
                icmd = cmd[:]
                cas[icmd] = Tk.Menu(rmenu, tearoff=0, takefocus=0)
        else:
            if txt == ' ------ ':
                rmenu.add_separator()
            else: rmenu.add_command(label=txt, command=cmd)

    rmenu.entryconfigure(0, label = "redemo", state = 'disabled')
    rmenu.tk_popup(e.x_root-3, e.y_root+3,entry="0")

    return "break"

def rClickbinder(r):
    for b in [ 'Text', 'Entry', 'Listbox', 'Label']:  # 
        r.bind_class(b, sequence='<Button-3>',
                          func=rClicker, add='')
#done

caveat time, I haven't tested this on nix and
I can imagine with some window managers
the right click menu won't unpost itself if
redemo loses focus. I'm sure someone will be
able to suggest a fix if this is the case.


e
please forward all spam to  uce at ftc.gov


________________________________________________________________
The best thing to hit the Internet in years - Juno SpeedBand!
Surf the Web up to FIVE TIMES FASTER!
Only $14.95/ month - visit www.juno.com to sign up today!




More information about the Python-list mailing list