Python Forum
[Tkinter] Paste Operation not working in Text Widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Paste Operation not working in Text Widget
#1
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/Final.py
Traceback (most recent call last):
  File "C:/Users/Ojotule/PycharmProjects/GUI/Final.py", line 325, in <module>
    root.mainloop()
  File "C:\Users\Ojotule\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1283, in mainloop
    self.tk.mainloop(n)
  File "C:\Users\Ojotule\PycharmProjects\GUI\test.py", line 41, in _proxy
    result = self.tk.call(cmd)
_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"
        self.tk.call("rename", self._w, self._orig)
        self.tk.createcommand(self._w, self._proxy)

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

        # 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.configure(yscrollcommand=self.vsb.set)
        self.text.tag_configure("bigfont", font=("Helvetica", "24", "bold"))
        self.linenumbers = TextLineNumbers(self, width=30)
        self.linenumbers.attach(self.text)

        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):
        self.linenumbers.redraw()


if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
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.
Thanks.
Reply
#2
you may want to take a look at this book https://www.packtpub.com/application-dev...nt-hotshot 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 ( http://pmw.sourceforge.net/ ).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,664 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Text widget inert mode on and off rfresh737 5 3,782 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  [Tkinter] Text.search() regexp not working rfresh737 11 4,910 Apr-16-2021, 06:56 PM
Last Post: rfresh737
  Line numbers in Text widget rfresh737 3 5,308 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,444 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,294 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to place global tk text widget in class or on canvas puje 1 2,281 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  [Tkinter] Override the paste function(copy from excel file - paste in separate tkinter entryes) AndreiV 3 4,557 Jun-05-2020, 04:46 PM
Last Post: AndreiV
  [Tkinter] manipulation of string in Text widget Stauricus 2 2,974 Feb-17-2020, 09:23 PM
Last Post: Stauricus
  how to insert image into Text widget Tkinter atlass218 5 9,924 Apr-17-2019, 05:28 AM
Last Post: atlass218

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020