Page 297 -
P. 297
control text fields
Read and write data to text fields
When someone enters text into an Entry widget, Python stores that text
somewhere in memory as a string. If you want to read the string, simply call
the widget’s get() method:
my_entry_field.get()
This will return the string “ice cream”.
But what if you want to change the contents of the widget, can you do that
too? Yes, you can: add text to the widget using the insert() method. It’s a
little more involved than just reading the text, because you need to say where
you want the text inserted:
my_entry_field.insert(0, "banana ")
This is the INDEX of
the insertion point. 0 1 2 345 6
Entry fields are indexed from 0.
You need to specify the index of the insertion point. Indexes in Entry fields
work just like indexes in strings: they begin at zero. So if you insert text at index
0, the new text will appear at the front of all the text already in the field. In the
same way, the delete() method lets you remove text from the field. You
might want to delete the entire contents of the field, so tkinter gives you a The final character
handy END symbol that lets you delete all the text like this: in the field is
indexed by END.
0 is the index of the first
This will delete the entire contents. character in the field. Poof!
my_entry_field.delete(0, END)
END is a special value
that represents the last All the text has gone.
character in the field.
The get(), insert(), and delete() methods give you complete
control over the contents of your Entry field.
But what about Text fields?
262 Chapter 8