Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Paste Operation not working in Text Widget
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.
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 ( ).

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] manipulation of string in Text widget Stauricus 2 151 Feb-17-2020, 09:23 PM
Last Post: Stauricus
  how to insert image into Text widget Tkinter atlass218 5 2,897 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] Lock Text Widget height Oxylium 3 635 Feb-14-2019, 10:13 PM
Last Post: woooee
  [Tkinter] update the content of Text widget atlass218 10 6,665 Dec-15-2018, 11:51 AM
Last Post: atlass218
  [Tkinter] Two text files in Text widget (python 3) atlass218 21 3,796 Nov-09-2018, 06:24 PM
Last Post: atlass218
  How to delete text from a tkinter Text widget? Tang 1 18,798 May-20-2018, 09:26 PM
Last Post: Larz60+
  [Tkinter] Class with text widget method AeranicusCascadia 3 4,031 Nov-14-2017, 11:33 PM
Last Post: AeranicusCascadia
  [Tkinter] Scroll bar height is not fixed with Text widget MeeranRizvi 2 6,624 Feb-06-2017, 12:24 PM
Last Post: MeeranRizvi
  [Tkinter] Entry widget : unable to get the text entered dlemper 2 10,027 Oct-11-2016, 12:32 AM
Last Post: dlemper

Forum Jump:

Users browsing this thread: 1 Guest(s)