Copy and paste text#
Copy text to the system clipboard, read it back, and work with the current
selection — all through methods every widget already has (they live on
Misc, the common base), so you can call them on app or any widget.
Copy and paste text#
Three methods cover the clipboard. Clear it, append your text, then it is available to other applications:
app.clipboard_clear()
app.clipboard_append("copied text")
Read what is on the clipboard with clipboard_get:
text = app.clipboard_get()
Note
clipboard_append adds to the clipboard; it does not replace. Call
clipboard_clear() first (as above) so you don’t concatenate onto whatever
was there. clipboard_get raises TclError when the clipboard is empty or
holds non-text data — guard it if that’s possible:
from tkinter import TclError
try:
text = app.clipboard_get()
except TclError:
text = ""
Note
On Linux the clipboard belongs to the app that set it, so what you copy is typically gone once your app exits — unless the user runs a clipboard manager that holds onto it. Windows and macOS hand the text to the system, where it outlives the process. Nothing to code around; just don’t rely on a copy surviving your own shutdown on Linux.
A copy action, wired to a button, is just those two calls:
def copy_result():
app.clipboard_clear()
app.clipboard_append(result_var.get())
ttk.Button(app, text="Copy", command=copy_result, bootstyle="secondary").pack()
To put it on a keyboard shortcut too, pick the key for the platform — macOS copies with Command-c, Windows and Linux with Control-c:
copy_key = "<Command-c>" if ttk.windowing_system(app) == "aqua" else "<Control-c>"
app.bind(copy_key, lambda e: copy_result())
Warning
Bind the shortcut on a specific widget or frame, not with bind_all. The
copy key is already the native copy gesture inside every Entry and Text —
bind_all fires on top of that handling, so a user who selects text in a
field and copies it gets your value on the clipboard instead of their
selection.
The current selection#
Entry, Text, and Listbox widgets track a selection — the highlighted range.
Read the selected text with selection_get:
entry.select_range(0, "end") # select all (Entry)
selected = entry.selection_get() # the highlighted text
By default selection_get reads the PRIMARY selection (the X11
“highlight” selection). Pass selection="CLIPBOARD" to read the clipboard
through the same call instead:
entry.selection_get(selection="CLIPBOARD") # same as clipboard_get()
Note
Reading your own app’s selection with selection_get works on every
platform. What differs is sharing it with other applications: on Linux
(X11), PRIMARY is a system-wide channel — highlighting text makes it pastable
with the middle mouse button. On Windows and macOS, Tk provides PRIMARY only
within your own application. For copy and paste that crosses application
boundaries, use the clipboard methods above everywhere.
See also
Events guide — binding the copy/paste shortcuts.
Menus guide — putting Copy and Paste on a menu with the right accelerator per platform.
Reference#
Clipboard —
clipboard_clear,clipboard_append, andclipboard_get.Selection —
selection_getand theselection=argument.Built-in virtual events — the
<<Copy>>/<<Cut>>/<<Paste>>events Entry and Text already handle.