Sep-11-2019, 01:28 PM
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:
Thanks.
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 1And 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 resultAnd 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.
