[Tkinter-discuss] Why can't I use the END index with my text widget in Tkinter?

Michael Lange klappnase at web.de
Sat Dec 11 10:58:03 CET 2010


Hi,

Thus spoketh irvinicus <irvinicus at yahoo.com> 
unto us on Fri, 10 Dec 2010 16:15:27 -0800 (PST):

> 
> I'm using the text widget to give ongoing updates to the user.  When
> something happens in the program, I want to add an update to the user
> in the text widget. 
> 
> But I can't figure out how to add each new update onto it's own new
> line. I've looked through the documentation, but I don't see any
> methods that would help me, so I figured I could just use the index END
> and use modifiers on it, but no such luck.
> 
> Here's my script (my italics, obviously):
> 
> from Tkinter import *  # I import Tkinter into my Python script.
> 
> ...  # Blah blah blah
> 
> mytextbox.insert(END + 1 lines, textfordisplay) 
> # The interpreter stops and tells me that lines is "invalid syntax".

Tkinter.END is simply a constant representing the string "end", so you
would have to write

    mytextbox.insert(END + " + 1 lines", textfordisplay)
or
    mytextbox.insert("end + 1 lines", textfordisplay)

However at least here (debian linux, tk-8.4), this does not work as
expected, when I repeatedly type

    mytextbox.insert('end + 1 lines', 'foobar')

the "foobars" are all appended to the end of the same last line. I guess
this because "end" means "end", and this method is only valid for other
indices, like "insert" and so on.

So if you want to add an empty line to the end of the text widget, the
most obvious way is to do:

    mytextbox.insert(END, os.linesep)

or, if you want to save a line,

    mytextbox.insert(END, os.linesep + textfordisplay)

but be careful, this might cause unexpected unicode errors if
"textfordisplay" contains any non-ascii characters.

For more details on text indices see:

http://www.tcl.tk/man/tcl8.4/TkCmd/text.htm#M18

I hope this helps

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Uncontrolled power will turn even saints into savages.  And we can all
be counted on to live down to our lowest impulses.
		-- Parmen, "Plato's Stepchildren", stardate 5784.3


More information about the Tkinter-discuss mailing list