tkinter "scrolledtext" copy paste doesn't work reliably tkinter "scrolledtext" copy paste doesn't work reliably tkinter tkinter

tkinter "scrolledtext" copy paste doesn't work reliably


You can also use pyperclip which supports Windows, Linux and Mac

import tkinter as tkimport pyperclipdef copy(event:tk.Event=None) -> str:    try:        text = text_widget.selection_get()        pyperclip.copy(text)    except tk.TclError:        pass    return "break"root = tk.Tk()text_widget = tk.Text(root)text_widget.pack()text_widget.bind("<Control-c>", copy)root.mainloop()


For a Windows only solution try this:

import tkinter as tkimport osdef copy(event:tk.Event=None) -> str:    try:        # Get the selected text        # Taken from: https://stackoverflow.com/a/4073612/11106801        text = text_widget.selection_get()        # Copy the text        # Inspired from: https://codegolf.stackexchange.com/a/111405        os.system("echo.%s|clip" % text)        print(f"{text!r} is in the clipboard")    # No selection was made:    except tk.TclError:        pass    # Stop tkinter's built in copy:    return "break"root = tk.Tk()text_widget = tk.Text(root)text_widget.pack()text_widget.bind("<Control-c>", copy)root.mainloop()

Basically I call my own copy function whenever the user presses Control-C. In that function I use the clip.exe program that is part of the OS to copy the text.

Note: my approach to copying data to the clipboard using os.system, isn't great as you can't copy | characters. I recommend looking here for better ways. You just need to replace that 1 line of code.


Using pyperclip and root.bind_all() we can solve the problem.

import tkinter, tkinter.simpledialog, tkinter.scrolledtext import pyperclip as cliproot = tkinter.Tk('test')text = tkinter.scrolledtext.ScrolledText(master=root, wrap='none')def _copy(event):   try:      string = text.selection_get()      clip.copy(string)   except:passroot.bind_all("<Control-c>",_copy)text.pack(side="top", fill="both", expand=True, padx=0, pady=0)text.insert(tkinter.END,'abc\ndef\nghi\njkl')root.mainloop()