Bottom Page

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.
Quote
#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/ ).
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to insert image into Text widget Tkinter atlass218 5 966 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] Lock Text Widget height Oxylium 3 399 Feb-14-2019, 10:13 PM
Last Post: woooee
  [Tkinter] update the content of Text widget atlass218 10 4,064 Dec-15-2018, 11:51 AM
Last Post: atlass218
  [Tkinter] Two text files in Text widget (python 3) atlass218 21 2,225 Nov-09-2018, 06:24 PM
Last Post: atlass218
  How to delete text from a tkinter Text widget? Tang 1 11,531 May-20-2018, 09:26 PM
Last Post: Larz60+
  [Tkinter] Class with text widget method AeranicusCascadia 3 3,108 Nov-14-2017, 11:33 PM
Last Post: AeranicusCascadia
  [Tkinter] Scroll bar height is not fixed with Text widget MeeranRizvi 2 5,086 Feb-06-2017, 12:24 PM
Last Post: MeeranRizvi
  [Tkinter] Entry widget : unable to get the text entered dlemper 2 7,721 Oct-11-2016, 12:32 AM
Last Post: dlemper

Forum Jump:


Users browsing this thread: 1 Guest(s)