[Tutor] Copy/Paste in Tkinter
Alan Gauld
alan.gauld at yahoo.co.uk
Sat Feb 4 05:32:31 EST 2017
On 04/02/17 00:24, Pooja Bhalode wrote:
> I am trying to write a simple Tkinter code in Python in order to
> demonstrate select/copy/paste/cut functionalities using keyboard keys and
> menu options.
You are going a fairly long winded way about it.
Rather than generating events use the Text/Entry
widget methods directly.
Check the documentation for the following methods:
Text.get(....) # copy
Text.selection_get(...) # copy a selection
Text.delete(...) # cut
Text.insert(...) # paste
To control selections look at the tag methods,
but that is more complex.
> def show_menu(e):
> w = e.widget
> # the_menu.entryconfigure("Select",command=lambda:
> w.event_generate("<<Select>>"))
> 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:
These should be in the create function. You don't need
to configure the command every time you show the menu.
> w.event_generate("<<Paste>>"))
> the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)
You should only need to call the tk.call() method very,
very rarely, there is usually a more direct way to do things.
> root = Tkinter.Tk()
> make_menu(root)
>
> e1 = Tkinter.Entry(); e1.pack()
> e2 = Tkinter.Entry(); e2.pack()
Shouldn't these Entries have a parent?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list