Tkinter Clipboard: Copy, Paste and Selected Text

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.

Copy and paste using the Tkinter clipboard


Tkinter Clipboard Methods 🔝

MethodPurpose
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)

Copy Text to the Clipboard 🔝

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.

Copy the Complete Content of a Text Widget

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)

Paste Text from the Clipboard 🔝

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.

Replace All Existing Text When Pasting

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.')

Complete Tkinter Copy and Paste Application 🔝

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()


Clipboard Management in Tkinter: Copy and Paste from Multiple Textboxes

Copy Only Selected Text 🔝

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.

Cut Selected Text 🔝

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.')

Copy Selected Text from Multiple Text Widgets 🔝

Copy selected text from multiple Tkinter Text widgets

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()

Clipboard vs Tk Selection 🔝

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.

MethodMeaning
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()

Reading CLIPBOARD through selection_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.

Handle Clipboard Errors 🔝

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.

Python Exception Handling

Built-in Copy, Cut and Paste Shortcuts 🔝

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:

  • the application needs visible Copy or Paste controls,
  • clipboard content must be processed before it is copied or inserted,
  • the data comes from another widget such as a Treeview, or
  • the application needs status messages or validation around clipboard operations.

For copying rows from a Treeview, see:

Copy Selected or All Treeview Rows

Common Questions about Tkinter Clipboard 🔝

How do I copy a string to the clipboard?

Clear the old clipboard and then append the new string:

root.clipboard_clear()
root.clipboard_append('Hello')

How do I paste clipboard text?

text=root.clipboard_get()

How do I copy selected text from a Text widget?

text=text_box.get(tk.SEL_FIRST, tk.SEL_LAST)

Why does copying selected text sometimes raise TclError?

SEL_FIRST and SEL_LAST exist only when text is selected. Handle tk.TclError when selection is optional.

Do I need root.update() after clipboard_append()?

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.

Summary of Tkinter Clipboard Operations 🔝

  • Use clipboard_clear() before replacing existing clipboard content.
  • Use clipboard_append(text) to copy text to the clipboard.
  • Use clipboard_get() to retrieve clipboard content.
  • Catch tk.TclError when clipboard text may be unavailable.
  • Use 'end-1c' to read the complete Text widget without its automatic final newline.
  • Use tk.SEL_FIRST and tk.SEL_LAST to access text selected inside a Text widget.
  • A Cut operation copies selected text and then deletes it from the widget.
  • A reusable function can handle selections from several Text widgets.
  • selection_get() belongs to Tk's selection mechanism and defaults to PRIMARY.
  • Use 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.
  • Tkinter Text and Entry widgets already support standard platform editing shortcuts.
Tkinter Text Treeview Copy Geometry Grid Layout




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer