Hit enter after type your search item

Python tkinter entry / single-line text box widget

Creating single line textbox in Python using tkinter library

To enable users to enter single-line text in Python GUI based application, you may use the tkinter entry widget.

  • The entry widget allows single-line text.
  • For multiple line text entry, use the text widget.
  • A user can enter numbers, strings, and mixed text as using the entry widget of tkinter library.

Syntax of creating an entry widget

w = Entry (parent, options)

Let us show you examples of creating single line textbox with simple and different available options.

An example of simple entry widget

In this example, we will create a label that is attached with the Entry widget. As you run the program, you will see:

Enter your name:

With the entry widget/textbox:


Output:

tkinter-entry-simple

Setting the background color of entry box

By using bg option of the entry widget, you may set the background color of the textbox.

You may provide a color name e.g. green, blue, red, etc., or a color value e.g. #FFF000.

See the example below.

Code:


Output:

tkinter-entry-bg-color

Setting borders of text box by bd option

The bd option enables us to set the borders of the Entry textbox. The default value is 2.

In the example below, we will set the border of two entry widgets. The first is 5 px and the second is 10 pixels for the demo only.

Python code:


Output:

tkinter-entry-borders

An example of foreground color/text color

For setting the foreground color (or text color) of the entry widget, use the fg option.

The default is black. However, as we set the dark background color in the above examples, you may want a lighter color of the text.

The example below has two entry fields. One with the default black color and the second is yellow color for the foreground (fg) option.

Code:


Output:

tkinter-entry-foregroun

Show entered text as * or some other character – password field

By using show option of the entry widget, you may display entered characters as the specified character.

For example, you want to take the user password and display * for the entered character.

The example below shows “^” for the first entry field and “*” for the second field.

Code:


Output:

tkinter-entry-show

An example of disabled entry fields

By using state=DISABLED option, the entry field is visible as grayed-out element in the form. The default value of the state is NORMAL.

The example below displays two entry fields. The first is disabled and the other is normal.

Code:


Output:

tkinter-entry-Disabled

An example of using entry in a message box

In this example, we will get the value of the entry field and display it in the tkinter message box.

For that, execute the program and enter text into the entry field. After this, press the button and it will display the text of the entry in a message box.

Code:


Sample output:

tkinter-entry-get-value

This div height required for enabling the sticky sidebar