I want to delete part of a text widget's content, using only character offset (or bytes if possible).
I know how to do it for lines, words, etc. Looked around a lot of documentations:
- https://www.tcl.tk/man/tcl8.6/TkCmd/text.html#M24
- https://tkdocs.com/tutorial/text.html
- https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/text-methods.html
- https://web.archive.org/web/20120112185338/http://effbot.org/tkinterbook/text.htm
Here is an example mre:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
txt = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse enim lorem, aliquam quis quam sit amet, pharetra porta lectus.
Nam commodo imperdiet sapien, in maximus nibh vestibulum nec.
Quisque rutrum massa eget viverra viverra. Vivamus hendrerit ultricies nibh, ac tincidunt nibh eleifend a. Nulla in dolor consequat, fermentum quam quis, euismod dui.
Nam at gravida nisi. Cras ut varius odio, viverra molestie arcu.
Pellentesque scelerisque eros sit amet sollicitudin venenatis.
Proin fermentum vestibulum risus, quis suscipit velit rutrum id.
Phasellus nisl justo, bibendum non dictum vel, fermentum quis ipsum.
Nunc rutrum nulla quam, ac pretium felis dictum in. Sed ut vestibulum risus, suscipit tempus enim.
Nunc a imperdiet augue.
Nullam iaculis consectetur sodales.
Praesent neque turpis, accumsan ultricies diam in, fermentum semper nibh.
Nullam eget aliquet urna, at interdum odio. Nulla in mi elementum, finibus risus aliquam, sodales ante.
Aenean ut tristique urna, sit amet condimentum quam. Mauris ac mollis nisi.
Proin rhoncus, ex venenatis varius sollicitudin, urna nibh fringilla sapien, eu porttitor felis urna eu mi.
Aliquam aliquam metus non lobortis consequat.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean id orci dui."""
text.insert(tk.INSERT, txt)
def test_delete(event=None):
text.delete() # change this line here
text.pack(fill="both", expand=1)
text.pack_propagate(0)
text.bind('<Control-e>', test_delete)
root.mainloop()
It display an example text inside a variable, inside a text widget. I use a single key binding to test some of the possible ways to do what I want on that piece of text.
I tried a lot of things, both from the documentation(s) and my own desperation:
text.delete(0.X)
: where X is any number. I thought since lines were1.0
, maybe using 0.X would work on chars only. It only work with a single char, regardless of what X is (even with a big number).text.delete(1.1, 1.3)
: This act on the same line, because I was trying to see if it would delete 3 chars in any direction on the same line. It delete 2 chars instead of 3, and it does so by omitting one char at the start of the first line, and delete 2 char after that.text.delete("end - 9c")
: only work at the end (last line), and omit 7 chars starting from EOF, and then delete a single char after that.text.delete(0.1, 0.2)
: Does not do anything. Same result for other0.X, 0.X
combination.
Example of what I try to achieve:
Using the example text above would take too long, so let's consider a smaller string, say "hello world". Now let's say we use an index that start with 1 (doesn't matter but make things easier to explain), the first char is "h" and the last one is "d". So say I use chars range such as "2-7", that would be "ello w". Say I want to do "1-8"? -> "hello wo", and now starting from the end, "11-2", "ello world".
This is basically similar to what f.tell() and f.seek() do. I want to do something like that but using only the content inside of the text widget, and then do something on those bytes/chars ranges (in the example above, I'm deleting them, etc).
command=lambda: offset()
can be more succinctly expressed ascommand=offset
– Octamerous