Tkinter.Text.index() puzzlement

Matthew Dixon Cowles matt at mondoinfo.com
Fri Apr 19 20:27:02 EDT 2002


On Fri, 19 Apr 2002 17:12:33 -0700, David Smith <nospam at nonesuch.com>
wrote:

Dear David,

>I am trying to use the "index" method to remember places in a Text
>window, but it doesn't seem to work properly.
[. . .]

>from Tkinter import *
>root = Tk()
>text = Text(root)
>text.pack()
>print "Beginning: end at %s" % text.index(END)
[. . .]

>The printed output says:
>Beginning: end at 2.0
[. . .]

>I don't understand why the first line is claimed to be number 2, nor
>why the character positions are all zero.  Is this normal?

That depends on your definition of normal <wink>. It's the way that
the widget is intended to work so it's normal in that sense. But the
person who wrote the Tk text widget may have a definition of the
normal behavior of a text widget that's different from yours and mine.

What's happening is that the widget is "helpfully" adding a newline
to the widget even if it's empty:

>>> from Tkinter import *
>>> r=Tk()
>>> t=Text(r)
>>> t.pack()
>>> t.get("1.0",END)
'\n'

Regards,
Matt



More information about the Python-list mailing list