Tkinter question: cut, copy and paste?

Russell E. Owen owen at astroNOJNK.washington.edu.invalid
Thu Aug 10 17:35:05 EDT 2000


I'm trying to figure out how to implement standard cut, copy and paste 
support for Tkinter's widgets (especially Enter and Text).

I happen to be working on a Mac (MacPython 1.5.2). The Edit menu is 
displayed, but the operations don't have any effect. I assume this is 
true of other platforms as well?

By experimenting I have found:

The existing Edit menu Cut, Copy and Paste items do send the expected 
virtual events <<Cut>>, <<Copy>> and <<Paste>>, as described in Harrison 
& McLennan's excellent "Effective Tcl/Tk Programming".

Harrison & McLennan go on to suggest binding to these events and calling 
"Tk library procedures" tk_textCut, tk_textCopy and tk_textPaste to 
command the widget do the right thing. This sounds perfect, but I have 
not found any way to get to these via Tkinter.

So, instead, I tried doing without these handy-sounding library 
procedures. So far it's been like hitting my head against a brick wall. 
It's easy enough to get the appropriate widget (the event is sent to a 
window, not a widget, but focus_get obtains the widget). But the trick 
is telling that widget what to do! I can get the currently selected text 
with selection_get, and presumably I can copy that to the clipboard, but 
what about Cut? There is no general command to delete the selected text 
(selection_clear eliminates the selection but doesn't affect the text). 
There are ways to do it in the Text widget, but they are specific to 
Text. I've not figured out any way to cut the selected text from the 
Entry widget. And having to hand-code each type of widget seems a bit 
crazy anyway.

So...how do you enable Cut, Copy and Paste in Tkinter?

-- Russell



More information about the Python-list mailing list