self.button2.grid(row=6, column=1)
self.text1.focus_set()
def convert(self):
"""Retrieve the text and convert to upper case"""
varText = self.text1.get()
varReplaced = varText.upper()
self.text1.delete(0, END)
self.text1.insert(END, varReplaced)
def clear(self):
"""Clear the Entry form"""
self.text1.delete(0,END)
self.text1.focus_set()
root = Tk()
root.title('Testing and Entry widget')
root.geometry('500x200')
app = Application(root)
app.mainloop()
The button1 widget links to the convert() method, which uses the get() method to retrieve
the text in the Entry widget, converts it to uppercase, and then uses the insert() method to place
the converted text back in the Entry widget to display it. Before it can do that, though, it uses the
delete() method to remove the original text from the Entry widget. The focus_set() method
is a handy tool: It allows you to tell the window which widget should get control of the cursor,
preventing our window user from having to click in the widget first.
Adding a Text Widget
For entering large amounts of text, you can use the Text widget. It provides for multiline text entry or
displaying multiple lines of text. The Text widget has the following syntax:
Click here to view code image
self.text1 = Text(self, options)
You can use quite a few options to control the size of the Text widget in the window and how it
formats the text contained within the display area. The most commonly used options are width and
height, which set the size of the Text widget area in the window. (width is defined in
characters and height in lines.)
As with to the Entry widget, you retrieve the text in a Text widget by using the get() method,
you remove text from the widget by using the delete() method, and you add text to the widget by
using the insert() method. However, there’s a bit of a twist to these methods in the Text widget.
Because the widget works with multiple lines of text, the index values you specify for the get(),
delete(), and insert() methods is not a single numeric value. It’s actually a text value that has
two parts:
"x.y"
In this case, x is the row location (starting at 1 ), and y is the column location (starting at 0 ). So, to
reference the first character in the Text widget, you use the index value "1.0".
Listing 18.7 shows the script1807.py file, which demonstrates the basic use of the Text
widget.