Tkinter can read and write text through the operating system clipboard. The three main methods are clipboard_clear(), clipboard_append() and clipboard_get().
These methods are useful when a Tkinter application needs Copy, Paste or Cut buttons, or when text must be exchanged between the application and other programs.

| Method | Purpose |
|---|---|
clipboard_clear() | Clears the current clipboard contents before new data is placed on it. |
clipboard_append(text) | Appends text to the Tk clipboard. |
clipboard_get() | Retrieves data from the clipboard. |
A typical copy operation uses:
root.clipboard_clear()
root.clipboard_append('Hello World')
To read the clipboard:
text=root.clipboard_get()
print(text)
clipboard_append() appends data. Calling clipboard_clear() first replaces the old clipboard text instead of adding to it.The following example copies a string to the system clipboard.
import tkinter as tk
root=tk.Tk()
root.clipboard_clear()
root.clipboard_append('Welcome to plus2net')
root.mainloop()
While the Tkinter application is running, the copied text can be pasted into another application.
A Tkinter Text widget always contains an extra newline at its final end index. Use 'end-1c' to read the actual text without that automatically added final newline.
def copy_text():
text=text_box.get('1.0', 'end-1c')
root.clipboard_clear()
root.clipboard_append(text)
strip() is not necessary here. strip() would also remove spaces or line breaks deliberately entered by the user.Use clipboard_get() to retrieve the current clipboard text.
def paste_text():
try:
text=root.clipboard_get()
text_box.insert(tk.INSERT, text)
except tk.TclError:
status.config(text='No text is available on the clipboard.')
tk.INSERT represents the current insertion cursor position in the Text widget, so the pasted content is inserted where the user is editing.
If the requirement is to replace the entire Text widget:
def replace_from_clipboard():
try:
text=root.clipboard_get()
text_box.delete('1.0', tk.END)
text_box.insert('1.0', text)
except tk.TclError:
status.config(text='Clipboard does not contain readable text.')
This example provides Copy, Paste and Clear buttons for one Text widget.
import tkinter as tk
def copy_text():
text=text_box.get('1.0', 'end-1c')
root.clipboard_clear()
root.clipboard_append(text)
status.config(text='Text copied.')
def paste_text():
try:
text=root.clipboard_get()
text_box.insert(tk.INSERT, text)
status.config(text='Text pasted.')
except tk.TclError:
status.config(text='Clipboard does not contain text.')
def clear_text():
text_box.delete('1.0', tk.END)
status.config(text='Text cleared.')
root=tk.Tk()
root.geometry('440x330')
root.title('Tkinter Clipboard - plus2net')
text_box=tk.Text(root, width=45, height=12, bg='#ffffe0', font=('Arial', 11))
text_box.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
tk.Button(root, text='Copy', command=copy_text, width=10).grid(row=1, column=0, padx=5)
tk.Button(root, text='Paste', command=paste_text, width=10).grid(row=1, column=1, padx=5)
tk.Button(root, text='Clear', command=clear_text, width=10).grid(row=1, column=2, padx=5)
status=tk.Label(root, text='')
status.grid(row=2, column=0, columnspan=3, pady=10)
root.mainloop()
When only highlighted text should be copied, use the Text widget's SEL_FIRST and SEL_LAST indexes.
def copy_selected():
try:
text=text_box.get(tk.SEL_FIRST, tk.SEL_LAST)
root.clipboard_clear()
root.clipboard_append(text)
except tk.TclError:
print('No text is selected.')
tk.SEL_FIRST is the start of the current selection and tk.SEL_LAST is its end. If there is no selection, Tkinter raises TclError.
text_widget.get(tk.SEL_FIRST, tk.SEL_LAST) is clearer than relying on the separate Tk selection API.A Cut operation first copies the selected text and then removes it from the Text widget.
def cut_selected():
try:
text=text_box.get(tk.SEL_FIRST, tk.SEL_LAST)
root.clipboard_clear()
root.clipboard_append(text)
text_box.delete(tk.SEL_FIRST, tk.SEL_LAST)
except tk.TclError:
print('Select some text first.')

A common function can receive the Text widget from which the selected text should be copied.
import tkinter as tk
def copy_selected(widget):
try:
text=widget.get(tk.SEL_FIRST, tk.SEL_LAST)
root.clipboard_clear()
root.clipboard_append(text)
status.config(text=f'Copied: {text}')
except tk.TclError:
status.config(text='Select text before copying.')
root=tk.Tk()
root.geometry('500x420')
root.title('Multiple Text Clipboard Example')
text_box1=tk.Text(root, width=45, height=5, bg='#e8f5e9')
text_box1.grid(row=0, column=0, padx=10, pady=10)
text_box1.insert('1.0', 'Select some text from the first Text widget.')
tk.Button(root, text='Copy Selection from Text 1', command=lambda: copy_selected(text_box1)).grid(row=1, column=0)
text_box2=tk.Text(root, width=45, height=5, bg='#ffecb3')
text_box2.grid(row=2, column=0, padx=10, pady=10)
text_box2.insert('1.0', 'Select some text from the second Text widget.')
tk.Button(root, text='Copy Selection from Text 2', command=lambda: copy_selected(text_box2)).grid(row=3, column=0)
status=tk.Label(root, text='')
status.grid(row=4, column=0, pady=10)
root.mainloop()
Tkinter also provides methods such as selection_get(), selection_clear() and selection_own(). These belong to Tk's selection mechanism and should not be presented as ordinary clipboard methods.
| Method | Meaning |
|---|---|
selection_get() | Gets the current Tk selection. Its default selection is PRIMARY, not the normal clipboard. |
selection_clear() | Clears ownership of the current Tk selection. |
selection_own() | Makes a widget the owner of a Tk selection. |
On systems such as X11/Linux, the PRIMARY selection can be separate from the normal CLIPBOARD selection.
For normal application Copy and Paste operations, prefer:
root.clipboard_clear()
root.clipboard_append(text)
text=root.clipboard_get()
If the selection API is specifically required, the normal clipboard can be requested explicitly:
text=root.selection_get(selection='CLIPBOARD')
However, clipboard_get() is clearer when the intention is simply to paste normal clipboard data.
clipboard_get() can raise tk.TclError when suitable clipboard data is unavailable.
try:
text=root.clipboard_get()
print(text)
except tk.TclError:
print('Clipboard does not contain readable text.')
The same exception can occur when attempting to access SEL_FIRST or SEL_LAST when no text is selected.
Tkinter Entry and Text widgets already provide standard editing behaviour through Tk's widget bindings. Users can normally use the operating system's familiar keyboard shortcuts for Copy, Cut and Paste.
Custom buttons are useful when:
For copying rows from a Treeview, see:
Copy Selected or All Treeview RowsClear the old clipboard and then append the new string:
root.clipboard_clear()
root.clipboard_append('Hello')
text=root.clipboard_get()
text=text_box.get(tk.SEL_FIRST, tk.SEL_LAST)
SEL_FIRST and SEL_LAST exist only when text is selected. Handle tk.TclError when selection is optional.
Not during a normal application that is already running mainloop(). The event loop continues processing Tk events. Clipboard behaviour after an application immediately exits can vary by operating system and windowing system.
clipboard_clear() before replacing existing clipboard content.clipboard_append(text) to copy text to the clipboard.clipboard_get() to retrieve clipboard content.tk.TclError when clipboard text may be unavailable.'end-1c' to read the complete Text widget without its automatic final newline.tk.SEL_FIRST and tk.SEL_LAST to access text selected inside a Text widget.selection_get() belongs to Tk's selection mechanism and defaults to PRIMARY.selection_get(selection='CLIPBOARD') only when working specifically through the selection API.selection_clear() does not mean deleting selected characters from a Text widget.selection_own() controls selection ownership rather than performing a normal Copy operation.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.