Python Tkinter – Text Entry Widget

Gathering input from a user in your GUI is something that will need to be done often, one basic way is to use a simple text entry field which i demonstrate below.

In this example I will be showing an example of user name and password entry fields, below is the base code we will start with which will define our grid, window size, and window name.


If you run that code you will see it just has a blank window with nothing in it. Now we will add a blank text entry widget in that field, that code is at lines 19 and 20 below.


As you can see this creates a basic text box that a user can click in and enter text.

The question now is how will we be able to tell the user what to put in this box? There are 2 ways, we could create a label and place it on any side of the entry widget or we could pre populate the entry widget with an indication of what were expecting. Either way is fine but for this example I will show you how to pre populate the entry widget with some text, the extra code for that is at line 20.


As you can see, now when we run the code we will have the entry field pre populated with the text ‘Enter Username’ so the user knows what is expected there.

Now lets say we would like to get the users password, we do not want to show what the user is typing in the box, it would be more appropriate to make every character the user enters a ‘ * ‘. That is very easy to do with an entry widget, below at lines 24-26 is the code to create another entry widget below our existing ‘username’ widget. Notice that in line 20 “show=’*’  ” is what will tell the entry widget to show asterisks in place of the characters the user types. To further assist the user I also pre-populated the widget with some spaces to indicate that it is for a password.


As you can see when we run this, as expected we have a second entry widget that will display any character typed into it as a ‘ * ‘.

The last thing I will demonstrate is a little more advanced and will allow you to clear out both of the widgets when the user focuses on them, either by clicking or tabbing, but will not clear out the widgets if the user refocuses on the widget after modifying its contents from default. The extra code for this is below.


To understanding what is happening here take a look at lines 31 and 37 first, what those are saying is when the focus is set to that widget call the ‘clear_widget’ function located between lines 11 and 17. If we now look at the ‘clear_widget’ function at lines 11-17, it has an if statement for each entry box we want to preform this on, the first comparison on line 14 is saying that if the ID of the ‘username_box’ widget is the same as the ID of the widget that currently has focus, and the contents of the ‘username_box’ widget is at its default of ‘Enter Username’ go ahead and clear out the box. This will prevent clearing out the widget if the user has the desired contents already put in there and they re-focus on the widget. Looking at lines 16 and 17 they do the exact same thing, the only difference is they do it for the ‘password_box’ widget and compare it against its default value.

Leave a Comment