Cut, Copy and Paste Text in Tkinter Entry Widgets

To copy text from one Tkinter Entry widget into another, select the text in e1, generate the <<Copy>> event and generate <<Paste>> on e2. These virtual events use Tkinter's normal clipboard handling.

e1.focus_set()
e1.selection_range(0, tk.END)  # Select all text in Entry e1
e1.event_generate("<<Copy>>")  # Copy the selected text
e2.event_generate("<<Paste>>") # Paste into Entry e2

Here, e1 is the source Entry and e2 is the destination Entry. Both are created in the complete application further down the page. For Cut, replace <<Copy>> with <<Cut>>.

Tkinter Entry form with Select All, Cut, Copy and Paste buttons transferring text between two input boxes
Select text in the first Entry, then cut or copy it and paste into the second Entry.

How the four buttons work

This desktop form has a source text field and a destination field. The buttons let the user select all text, move it to the clipboard, copy it without deleting it, and paste it into the second field.

ButtonActionResult
Select Allselection_range(0, tk.END)Highlights the source text
Cut<<Cut>>Copies selected text to the clipboard and removes it from the source
Copy<<Copy>>Copies selected text without removing it
Paste<<Paste>>Inserts clipboard text into the destination

Select, Cut, Copy and Paste Commands

Entry is a single-line text input widget. The following commands are called when the user presses the form's buttons. The same approach works with any normal editable tk.Entry field.

Selecting all text inside an Entry widget

First, give the source field keyboard focus, and then select its content from index 0 to tk.END. selection_range() and the shorter alias select_range() both select Entry text.

e1.focus_set()
e1.selection_range(0, tk.END)

Use e1.selection_present() to check whether the user has selected any text before attempting to copy only a selection. Selecting the text is useful for both the Cut and Copy operations.

Cut the selected text

When the source Entry has a selection, Tkinter's <<Cut>> virtual event copies that selection to the clipboard and deletes it from the source field.

e1.event_generate("<<Cut>>")

The event method returns normally; do not put a trailing comma after the call. A trailing comma would form a tuple expression rather than being part of the clipboard action.

Copy selected text without deleting it

The <<Copy>> virtual event places the selection onto the system clipboard and leaves the original content in the first Entry.

e1.event_generate("<<Copy>>")

Paste the clipboard into another Entry

Use the destination Entry e2 for the Paste event. The clipboard text is inserted at the cursor or replaces the destination's selected text, following Tk's normal Entry editing behavior.

e2.focus_set()
e2.event_generate("<<Paste>>")

Note: Paste does not automatically replace all existing text in the destination. Use the separate replace-all example below if that is the intended behavior.

Complete Working Tkinter Cut, Copy and Paste Program

This example displays two Entry boxes and four buttons. It uses functions as the button commands so it is easier to understand and extend. The source Entry starts with sample text, making the example ready to test as soon as the window opens.

import tkinter as tk

my_w = tk.Tk()
my_w.title("plus2net.com - Tkinter Clipboard Demo")
my_w.geometry("600x250")

def select_all():
    e1.focus_set()
    e1.selection_range(0, tk.END)

def cut_text():
    e1.focus_set()
    e1.event_generate("<<Cut>>")

def copy_text():
    e1.focus_set()
    e1.event_generate("<<Copy>>")

def paste_text():
    e2.focus_set()
    e2.event_generate("<<Paste>>")

label = tk.Label(my_w, text="Name", font=("Arial", 12))
label.grid(row=0, column=0, padx=10, pady=20)

e1 = tk.Entry(my_w, font=("Arial", 12), width=35, bg="yellow")
e1.grid(row=0, column=1, columnspan=4, padx=5)
e1.insert(0, "Python Tkinter clipboard example")

b1 = tk.Button(my_w, text="Select All", command=select_all,
               font=("Arial", 11), bg="lightgreen")
b1.grid(row=1, column=1, padx=3, pady=8)

b2 = tk.Button(my_w, text="Cut", command=cut_text,
               font=("Arial", 11), bg="lightyellow")
b2.grid(row=1, column=2, padx=3)

b3 = tk.Button(my_w, text="Copy", command=copy_text,
               font=("Arial", 11), bg="lightblue")
b3.grid(row=1, column=3, padx=3)

b4 = tk.Button(my_w, text="Paste", command=paste_text,
               font=("Arial", 11), bg="cyan")
b4.grid(row=1, column=4, padx=3)

e2 = tk.Entry(my_w, font=("Arial", 12), width=35, bg="yellow")
e2.grid(row=2, column=1, columnspan=4, padx=5, pady=20)

my_w.mainloop()

Test the application: Click Select All, then Copy, and finally Paste. The second Entry should receive the same text. Click Cut after selecting the source text to move rather than duplicate it. All clipboard actions are handled by the local desktop GUI.

Running the program: Save it as a Python .py file and run it on a desktop with Tkinter and a graphical display available. It is not intended for Google Colab, because Colab does not provide a local desktop Tkinter window.

Video: Tkinter Entry Select, Cut, Copy and Paste

Tkinter select all data from Entry box and copy or cut to clipboard to paste in another Entry box

For more background on Entry widgets, you can also watch the original Tkinter Entry widget video.

Tkinter Clipboard: Virtual Events vs. Direct Methods

Tkinter supports both virtual editing events and direct clipboard methods. Use the virtual events for standard user-interface editing, especially when working with selected text. Use the direct clipboard methods when you want to copy an entire value without selecting it, replace content programmatically, or handle clipboard errors explicitly.

ApproachCommandsWhen to use
Virtual events<<Copy>>, <<Cut>>, <<Paste>>Standard copying and pasting of a text selection
Direct clipboardclipboard_clear(), clipboard_append()Copying all input or a generated value without selection
Reading the clipboardclipboard_get()Validating or controlling where the pasted text goes
Entry editingdelete(), insert(), get()Replacing or inspecting a field programmatically

Copy an entire Entry and paste at the cursor

This variation copies the full value returned by e1.get(), even if the user has not selected any text. It also shows how to handle an empty or unavailable text clipboard when pasting. The functions can be added to the complete program after the widgets are created.

def copy_entire_entry():
    # Copy all Entry text without selecting it first.
    text = e1.get()
    my_w.clipboard_clear()
    my_w.clipboard_append(text)
    my_w.update()  # Process clipboard events while the app is running.

def paste_clipboard_at_cursor():
    try:
        text = my_w.clipboard_get()
    except tk.TclError:
        print("There is no available text on the clipboard")
        return
    e2.insert(tk.INSERT, text)

clipboard_get() retrieves text when compatible clipboard data is available. Catching tk.TclError avoids an unhandled error when no readable text exists.

Replace all existing destination text when pasting

If you want to overwrite the destination field instead of inserting at the cursor, delete its contents first and insert the clipboard text from position zero.

def paste_and_replace():
    try:
        text = my_w.clipboard_get()
    except tk.TclError:
        print("Clipboard does not contain readable text")
        return

    e2.delete(0, tk.END)  # Remove existing destination text.
    e2.insert(0, text)   # Insert the copied text at the start.

Read the clipboard before clearing the destination so an unavailable clipboard does not accidentally erase the existing value.

Additional Entry Clipboard Examples

Copy only when text is selected

When a user clicks the Copy button without selecting anything, selection_present() can provide a simple check before generating the event.

def copy_if_selected():
    if e1.selection_present():
        e1.focus_set()
        e1.event_generate("<<Copy>>")
    else:
        print("Select some text in the first Entry widget")

Find the selected portion of an Entry

The indexes tk.SEL_FIRST and tk.SEL_LAST identify the range of the current Entry selection. They must only be read when a selection exists; otherwise Tk raises an error.

# Read only the currently selected portion of an Entry widget.
if e1.selection_present():
    first = e1.index(tk.SEL_FIRST)
    last = e1.index(tk.SEL_LAST)
    selected_text = e1.get()[first:last]
    print("Selected:", selected_text)

Clear the destination Entry without changing the clipboard

Use delete(0, tk.END) to clear a single-line Entry. This does not remove data already stored in the operating system clipboard.

# Clear the destination without affecting clipboard contents.
e2.delete(0, tk.END)

Enable Ctrl+A to select the text

For Windows and Linux desktop applications, a Ctrl+A binding can help users select all text without clicking the Select All button. Keyboard shortcuts may differ on macOS, where Command-based bindings are commonly used.

def select_all_from_keyboard(event):
    event.widget.selection_range(0, tk.END)
    return "break"  # Prevent another binding processing this event.

e1.bind("<Control-a>", select_all_from_keyboard)
e2.bind("<Control-a>", select_all_from_keyboard)

Copy from whichever Entry currently has focus

In larger forms with multiple Entry fields, focus_get() tells you which widget currently receives keyboard input. The following button handler copies only a selected range in the currently focused tk.Entry, rather than always using e1.

def copy_from_focused_entry():
    widget = my_w.focus_get()
    if isinstance(widget, tk.Entry) and widget.selection_present():
        widget.event_generate("<<Copy>>")
    else:
        print("Focus an Entry widget and select text to copy")

Buttons and other controls can change keyboard focus. If you rely on the previously active Entry, store that widget reference before focus moves, or bind the action directly to the Entry.

Common Errors and Troubleshooting

ProblemLikely causeWhat to check
Copy button appears to do nothingNo text is selected in the source EntryUse Select All or call selection_present() before copying
Cut does not remove the inputNo selection, or the widget is not editableCheck the selection and the Entry's state
Paste inserts at the wrong positionThe insertion cursor is not at the expected locationSet the insertion position or use delete() followed by insert()
_tkinter.TclError while reading selectionSelection indexes were requested with no selected textCall selection_present() first
_tkinter.TclError from clipboardNo compatible clipboard text is availableHandle clipboard_get() inside try/except
No Tkinter window opensRunning in a headless environment without a GUI displayRun the script on a graphical desktop with Tkinter installed
Buttons or Entry fields are clippedWindow too narrow for the chosen fonts and grid layoutIncrease geometry or adjust Entry width and button spacing

Tip: Standard keyboard shortcuts and the clipboard can behave differently by operating system and desktop environment. Test the final interface on each target platform. Avoid copying sensitive input values to the clipboard unless the user expects that behavior.

Frequently Asked Questions

How do I select all text in a Tkinter Entry?

Use entry.selection_range(0, tk.END). Calling entry.focus_set() first makes the selected text easier to see and edit.

What is the difference between Cut and Copy?

<<Copy>> copies selected Entry text and leaves it in place. <<Cut>> copies the selection and also removes it from an editable Entry.

Can I copy Entry text without selecting it?

Yes. Read the text with entry.get() and write it to the clipboard with clipboard_clear() followed by clipboard_append().

Does Paste replace the existing text in an Entry?

Normally it inserts text at the cursor, replacing a selected range if one exists. To replace all content, use entry.delete(0, tk.END) and entry.insert(0, clipboard_text).

How is copying text in Tkinter Text widgets different?

A Text widget supports multiple lines and uses indexes such as 1.0 and tk.END. See the Cut, Copy and Paste in Tkinter Text widgets tutorial for those methods.

Do I need to install a third-party clipboard library?

No. For normal Tkinter Entry cut, copy and paste, the standard tkinter module provides the required virtual events and clipboard methods.

Back to top ↑




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