Question

You know how in Tkinter, you can do something like this:

self.scrolled_text.replace("insert-1c", INSERT, myText);

and it'll replace the character left of the current position with myText?

Well, where are the specifics of what all you can put in place of "insert-1c" documented?

I know -1c means minus one character. You can do -2c for minus two, or +5c for plus five characters. I only know that because I examined someone's code after spending some time on Google (not because I found documentation).

Is there more to know, or is that it? Anyway, I'd love to be able to do words instead of characters without doing a search and finding the whitespace. They should have a documentation wiki for stuff like this.


EDIT: I actually found more than one wiki for Tkinter, one of which discusses this a little, ironically. There's an official python.org one, too (although I don't know if either of these are the one that discussed this particular topic):


EDIT: Since I've gotten my answers, I'll share some things I learned that may help some people. ScrolledText.get(index1, index2) is pretty much essential, if you want to do comparisons. ScrolledText.index(INSERT) will return the row and column of the INSERT, as Bryan Oakley said in his answer in different words (e.g. "1.0" for row 1, column 0, or the beginning of the file).

EDIT2: ScrolledText.compare(index1, "==", index2) takes less code for comparisons, as one might imagine. I didn't know about this at the time.

Here's the code I wrote for emulating the control-backspace and control-delete functionality just how I like it.

Control-delete (self.cur_scroll() is my ScrolledText widget; "  " contains a space and a non-breaking space):

insert=self.cur_scroll().get(INSERT);
if insert in "  " and insert!="":
    while insert in "  " and insert!="":
        self.cur_scroll().delete(INSERT);
        insert=self.cur_scroll().get(INSERT);
elif insert not in "\n\t  .?!,@#…¿/\\\"'—–":
    while insert not in "\n\t  .?!,@#…¿/\\\"'—–":
        self.cur_scroll().delete(INSERT);
        insert=self.cur_scroll().get(INSERT);
    if insert in "  ":
        while insert in "  " and insert!="":
            self.cur_scroll().delete(INSERT);
            insert=self.cur_scroll().get(INSERT);
else:
    if insert in "\n\t" and insert!="":
        if self.cur_scroll().get(INSERT, END).strip()=="" and self.cur_scroll().index(INSERT)!=self.cur_scroll().index(END):
            self.cur_scroll().delete(INSERT, END);
        else:
            while insert in "\n\t  " and insert!="":
                self.cur_scroll().delete(INSERT);
                insert=self.cur_scroll().get(INSERT);
    else:
        self.cur_scroll().delete(INSERT);

Control-backspace:

if self.cur_scroll().index("insert-1c")!="1.0" and self.cur_scroll().index(INSERT)!="1.0":
    if self.cur_scroll().index("insert wordstart-1c")=="1.0":
        i=0;
        while self.cur_scroll().index(INSERT)!="1.0" and i<25:
            self.cur_scroll().delete("insert-1c");
            i+=1;
    else:
        insertm1c=self.cur_scroll().get("insert-1c");
        if insertm1c in "\n\t.?!,@#…¿/\\\"'—–" and insertm1c!="":
            i=0;
            while (insertm1c in "\n\t.?!,@#…¿/\\\"'—–" and insertm1c!="") and i<25:
                self.cur_scroll().delete("insert-1c");
                insertm1c=self.cur_scroll().get("insert-1c");
                i+=1;
        else:
            while insertm1c not in "\n\t  .?!,@#…¿/\\\"'—–" and insertm1c!="":
                self.cur_scroll().delete("insert-1c");
                insertm1c=self.cur_scroll().get("insert-1c");
            while insertm1c in "  " and insertm1c!="":
                self.cur_scroll().delete("insert-1c");
                insertm1c=self.cur_scroll().get("insert-1c");
Was it helpful?

Solution

See the "Indexes" section of this page: http://effbot.org/tkinterbook/text.htm. The definitive documentation is here: http://tcl.tk/man/tcl8.5/TkCmd/text.htm#M7

The short answer is, you can put any index of the form "line.char", and the special value "end". You can modify these with things like -1c ("minus one character") and "wordend" and several others. You can also use marks, such as the built-in mark named "insert".

OTHER TIPS

There's always more.

You can check out the references listed here: https://stackoverflow.com/tags/tkinter/info

Here's the official Text doc: http://www.tcl.tk/man/tcl8.5/TkCmd/text.htm

And to answer your question about whole words, consider this example:

def replace():                                        # deletes whole word where insert
    text.delete('insert wordstart', 'insert wordend') # cursor is found

root=Tk()

text = Text(root)
text.pack()
text.insert(END, 'Hello world')

Button(root, text='Delete word', command=replace).pack()

root.mainloop()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top