rlght click menu missing in most Tk apps

E

eltronic

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.

there will be a line something like
root=Tk.mainloop() #or Tkinter.mainloop()

in epydoc it was necessary to look into the gui class
and find one of the attributes has the root.
add this before the root= line at the same indentation.
rCbinder(gui._rootframe) #or root or whatever
gui.mainloop() #or root. or= or whatever .|=

hopefully authors or dedicated users of Tk apps will see how simple
it is to add this basic service and will add the code themselves.
in Qt you get rightclick menu's for free with all entry widgets.
for gtk and wx, I don't know, probably is similar to one or the other.

in pydoc it was necessary to add a search button also
bind like the <return> in the Entry widget
so pydoc could be entirely mouse driven.
self.search_btn = Tkinter.Button(self.search_frm,
text='search', pady=0, command=self.search)
....
self.search_btn.pack(side='right')
....
self.search_ent.bind('<Button-3>',rClicker, add='')
self.result_lst.bind('<Button-3>',rClicker, add='')
in pydoc there are only 2 entry widgets so it
was simpler to just bind them to rClicker directly
rClicker takes an event as a parameter as default.
if I ever get a sourceforge id I will post a patch.

I found some of the original clues in Idle, which
for unknown reasons doesn't provide rightclick copy & paste.
I wouldn't take the lack of outcry as confirmation that
noone is missing this basic expected convenience.
you should assume some people think your app is broken
and possibly have quit using and recommending it.
certainly they rclick numerous times and are annoyed when
nothing familiar happens.
You can get fancy and disable or remove choices like don't
display cut or delete where they don't make sense.
you can add application specific shortcuts too, per widget,
by checking the event._name attribute or type,
you do name your widgets don't you?
adding cascading menus is just like for pulldown menus.
I've left all that out to simplify and shorten this posting.

here are the 2 functions to add-------------
def rClicker(e):
''' right click context menu for all Tk Entry and Text widgets
'''
import Tkinter
try:
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>')

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)),
]

rmenu = Tkinter.Menu(None, tearoff=0, takefocus=0)

for (txt, cmd) in nclst:
if txt == ' ------ ':
rmenu.add_separator()
else:
rmenu.add_command(label=txt, command=cmd)

rmenu.entryconfigure(0, state = 'disabled')

rmenu.tk_popup(e.x_root-3, e.y_root+3,entry="0")

except Tkinter.TclError:
print ' - rClick menu, something wrong'
pass

return "break"

def rClickbinder(r):
import Tkinter
try:
for b in [ 'Text', 'Entry', 'Listbox', 'Label']: #
r.bind_class(b, sequence='<Button-3>',
func=rClicker, add='')
except Tkinter.TclError:
print ' - rClickbinder, something wrong'
pass

#I don't know this is the best way to go. but,
#don't let the perfect be the enemy of the good enough


#e


________________________________________________________________
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!
 
S

Stewart Midwinter

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 how it is used?

thanks
S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top