15-7. Entry widget

An Entry widget is a rectangular area within a graphical user interface where users can input text. It serves as a means to collect user input in a GUI program. Typically, an Entry widget is accompanied by a button for submitting the entered data. The callback function attached to the button retrieves the data from the Entry widget(s) and performs necessary processing. To retrieve data from an Entry widget, the get() method is employed, which returns the entered text as a string.

Example 1

This example demonstrates how to input two numbers, add them together, and display the result using an Entry widget.

Output:

The “Number 1” label is displayed, and an entry widget appears immediately to its right. On the next line, the “Number 2” label is displayed, and another entry widget appears immediately to its right. On the following line, the “Sum” label is displayed, and yet another entry widget appears immediately to its right. Below the third entry widget, a button labeled “Click to add” is displayed.

After entering two numbers, click Sum button.

The “Number 1” label is displayed, and an entry widget appears immediately to its right. On the next line, the “Number 2” label is displayed, and another entry widget appears immediately to its right. On the following line, the “Sum” label is displayed, and yet another entry widget appears immediately to its right. Below the third entry widget, a button labeled “Click to add” is displayed. 10 is entered to the first entry widget, 20 is entered to the second entry widget, and the 30 is shown in the third entry widget.

 

Example 2: StringVar

To display data, initiate a StringVar object and then establish a Label widget, linking it to the StringVar object. Following this, any data stored within the StringVar object will be promptly showcased in the Label widget.

Output:

The “Number 1” label is displayed, and an entry widget appears immediately to its right and 10 is entered. On the next line, the “Number 2” label is displayed, and another entry widget appears immediately to its right and 20 is entered. On the following line, the “Sum” label is displayed, and a label with 30 is shown immediately to its right. Below the third entry widget, a button labeled “Click to add” is displayed.