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 .

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 ")

ThCisthe INDE* of the insertion point-

banana ice cream

2345 6

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 handy END symbol that lets you delete all the text like this:

The f inal character rn the f ield is indexed by ENP-~\

0 is the index of the f irst This will delete the entire Contents praeter in the field-

my entry field.delete(0, END) "A ~ ' J ////// /////.{j/^jC'- ^

END is a special value -that represents the last character in the field.

All the text has gone.

pootr

The get (), insert (), and delete () methods give you complete control over the contents of your Entry field.

But what about Text fields?

Was this article helpful?

0 0

Post a comment