cut & paste text between tkinter widgets

jepler at unpythonic.net jepler at unpythonic.net
Thu Aug 4 00:06:30 CEST 2005


Here's some code that gives a cut-copy-paste pop-up window on all Entry widgets
in an application.

This code is released into the public domain.

Jeff Epler
#------------------------------------------------------------------------
import Tkinter

def make_menu(w):
    global the_menu
    the_menu = Tkinter.Menu(w, tearoff=0)
    the_menu.add_command(label="Cut")
    the_menu.add_command(label="Copy")
    the_menu.add_command(label="Paste")

def show_menu(e):
    w = e.widget
    the_menu.entryconfigure("Cut",
        command=lambda: w.event_generate("<<Cut>>"))
    the_menu.entryconfigure("Copy",
        command=lambda: w.event_generate("<<Copy>>"))
    the_menu.entryconfigure("Paste",
        command=lambda: w.event_generate("<<Paste>>"))
    the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)

t = Tkinter.Tk()
make_menu(t)

e1 = Tkinter.Entry(); e1.pack()
e2 = Tkinter.Entry(); e2.pack()
e1.bind_class("Entry", "<Button-3><ButtonRelease-3>", show_menu)

t.mainloop()
#------------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050803/2fab42da/attachment.sig>


More information about the Python-list mailing list