Tkinter.Text.index() puzzlement

David Smith nospam at nonesuch.com
Fri Apr 19 20:12:33 EDT 2002


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


from Tkinter import *
root = Tk()
text = Text(root)
text.pack()
print "Beginning: end at %s" % text.index(END)
text.insert(END, "Line 1")
print "After line 1: end at %s" % text.index(END)
text.insert(END, "\nLine 2")
print "After line 2: end at %s" % text.index(END)
text.insert(END, "\nLine 3")
print "After line 3: end at %s" % text.index(END)
text.insert(END, "\n")
print "Beginning line 4: end at %s" % text.index(END)
root.mainloop()


This fragment puts up the text window, which displays:
Line 1
Line 2
Line 3

The printed output says:
Beginning: end at 2.0
After line 1: end at 2.0
After line 2: end at 3.0
After line 3: end at 4.0
Beginning line 4: end at 5.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?

This is with Python 2.1 under Debian.

-- 
David Smith
drs at labs dot agilent dot com  <- hopefully doesn't trigger spambots



More information about the Python-list mailing list