By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,396 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

rlght click menu missing in most Tk apps

P: n/a
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,
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
def rClick_Copy(e, apnd=0):

def rClick_Cut(e):

def rClick_Paste(e):


(' ',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_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'

return "break"

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

#I don't know this is the best way to go. but,
#don't let the perfect be the enemy of the good enough
__________________________________________________ ______________
The best thing to hit the Internet in years - Juno SpeedBand!
Surf the Web up to FIVE TIMES FASTER!
Only $14.95/ month - visit to sign up today!

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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 how it is used?


Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.