[Tutor] The Tkinter Text widget and .get()

Alan Gauld alan.gauld at freenet.co.uk
Mon Feb 7 01:22:33 CET 2005


> As I understand, .get() has to get an index argument to get the text
> from the Text index...

Thats true.

> The problem is that I dont realy understand what is this index thing

Thats not surprising the Text widget index in Tk (its not really a
Tkinter thing, its part of the underlying Tk toolkit...) is just
a little bit weird!

> and what index do I need to give to the function so I'll get all the
> text in the widget.

THe magic incantation (or one option, there are various ways) is:

txt.get(1.0,END)

Where 1.0 means first line, zeroth character (ie before the first!)
is the starting position and END is the ending position.

The index is a conceptual cursor that sits *between* characters.
Thus for a line like:

Here is a line

If we only wanted the second word we'd use

get(1.5, 1.7)

This is explained in both the Tkinter documentation (but slightly
vaguely), and,more precisely in the Tk documentation. AS with most
things Python the best bet is to experiment at the >>> prompt
till you get it right!

There are some examples of using the Text widget in my tutorial
in both the Event Driven Programming and Case Study topics.
Specifically the doReset() method inthe case study uses indexing
to delete the text in a Text box and doAnalyze shown text
being appended (inserted at END).

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list