Python Forum
[Tkinter] Paste Operation not working in Text Widget - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: GUI (
+--- Thread: [Tkinter] Paste Operation not working in Text Widget (/Thread-Tkinter-Paste-Operation-not-working-in-Text-Widget)

Paste Operation not working in Text Widget - Code_Enthusiast - Sep-11-2019

Good day, I'm new to programming with Tkinter and I'm working on a text editor.
I recently used a code posted online that creates a custom Text class that generates a <<Change>> event whenever text is inserted or deleted, or when the view is scrolled (this class was used to implement a line number feature in my editor).
I recently discovered that the 'Ctrl+V' (paste operation) returns an error anytime I use the shortcut.
I've created a callback for a paste button and I tried to overwrite the 'Ctrl+V' event using the bind() method with this callback and I still got an error.

Below is the error message:

C:\Users\Ojotule\PycharmProjects\GUI\venv\Scripts\python.exe C:/Users/Ojotule/PycharmProjects/GUI/
Traceback (most recent call last):
  File "C:/Users/Ojotule/PycharmProjects/GUI/", line 325, in <module>
  File "C:\Users\Ojotule\AppData\Local\Programs\Python\Python37\lib\tkinter\", line 1283, in mainloop
  File "C:\Users\Ojotule\PycharmProjects\GUI\", line 41, in _proxy
    result =
_tkinter.TclError: text doesn't contain any characters tagged with "sel"

Process finished with exit code 1
And here is the custom text class:

class CustomText(tk.Text):
    def __init__(self, *args, **kwargs):
        tk.Text.__init__(self, *args, **kwargs)

        # create a proxy for the underlying widget
        self._orig = self._w + "_orig""rename", self._w, self._orig), self._proxy)

    def _proxy(self, *args):
        # let the actual widget perform the requested action
        cmd = (self._orig,) + args
        result =

        # generate an event if something was added or deleted,
        # or the cursor position changed
        if (args[0] in ("insert", "replace", "delete") or
            args[0:3] == ("mark", "set", "insert") or
            args[0:2] == ("xview", "moveto") or
            args[0:2] == ("xview", "scroll") or
            args[0:2] == ("yview", "moveto") or
            args[0:2] == ("yview", "scroll")
            self.event_generate("<<Change>>", when="tail")

        # return what the actual widget returned
        return result
And this is where the main GUI was created:

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.text = CustomText(self)
        self.vsb = tk.Scrollbar(orient="vertical", command=self.text.yview)
        self.text.tag_configure("bigfont", font=("Helvetica", "24", "bold"))
        self.linenumbers = TextLineNumbers(self, width=30)

        self.vsb.pack(side="right", fill="y")
        self.linenumbers.pack(side="left", fill="y")
        self.text.pack(side="right", fill="both", expand=True)

        self.text.bind("<<Change>>", self._on_change)
        self.text.bind("<Configure>", self._on_change)

    def _on_change(self, event):

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
Please, any help would be greatly appreciated as I would like to understand why I got this error and what I can do to solve this problem.

RE: Paste Operation not working in Text Widget - Larz60+ - Sep-11-2019

you may want to take a look at this book which you can get an ebook for 10 bucks. The main project which spans I think the first seven chapters is a full featured text editor done in tkinter, with tkinter pmw megawidgets ( ).