Python – Tkinter Entry Widget

In this post I will be outlining the Tkinter Entry widget and the various ways you can use it. The Entry widget is often used to display or enter a line of text and is a standard Tkinter widget so you will not need to install anything special to your Python environment to use it.

Below is the base code I will be referencing throughout this post, It is simply a blank window an entry widget defined and placed on the grid.

Code Breakdown:

Lines 3&4: Import the modules we will be using from Tkinter.

Lines 6&7: Create a window and define its default size.

Lines 9-13: Give weight to a grid 10 cells x 10 cells, learn more about that here.

Lines 15-16: Creates our entry widget, attaches it to our ‘root’ object and places it in row 4 column 4 in our grid.

Line 18: Starts the main loop to draw the program.

 

Getting data from Entry widget

The entry widget is not very useful if we can not put information into it or extract information out of it. To get information out of our widget we can use the ‘get()’ method, below I have added to my base code to include a button that calls a function, and in that function it will gather the text in the entry widget and print it to the terminal.

Code Breakdown:

Lines 7-9: This is our function that when called will gather whatever is in our entry widget ‘entry_widget’ which is defined on lines 20&21 and print it to our terminal window.

Lines 23-25: Creates a button, configures it to call our ‘get_entry_data’ function when pressed and places it on or grid. The button is what we are using to call the action of retrieving the data from our entry widget. Learn more about buttons here

Looking at this code we can see that the action of getting the data from our entry widget is in line 8 which is using the ‘get()’ method to extract the data from the entry widget. It is important to not get confused with the extra code defining our button and our function, those are simply ways to organize our code and our interface. The main thing you should take away is what is happening in line 8.

 

Removing data from Entry widget

Lets say we want to use the same code as above but instead of just printing the contents of our entry widget so the user can enter something new. For this we can use the ‘delete()’ method which when specified with a start index and an end index will delete the specified characters.

 

Code Breakdown:

Line 6: I added a new import from tkinter

Line 12: After getting the data from the entry widget in line 9, line 12 will delete all data starting at the index of ‘0’ and go until the end of data (END) to clear out the entire widget

If you run this code above you will see after entring something in the entry widget in hitting our button it will clear the widget out and still print the data to the screen. One thing you can do is manipulate the indexes in line 12, for instance if you were to change ‘END’ to ‘1’ like the code snippet below it would only delete the first character of whatever was in your entry widget when the button was pressed.

 

Inserting data into Entry widget

We now know how to retrieve and delete information from our entry widget but what if we want our program to enter some default data into out entry widget? We can do this using the ‘insert()’ method, in the code below I show you how to have the script enter some default text into our entry widget.

 

Code Breakdown:

Looking we can see the only line of code that we added was at line 27 which uses the insert method which is saying that starting at an index of 0 enter the text “Enter Text Here”.

With knowledge of how the ‘delete()’ and ‘insert()’ methods work it is not that hard to see that if you wanted you could enter data anywhere within the entry widget, lets say that our entry widget already had text in it and we wanted to append our text to the end we could use ‘END’ as our index versus using 0.

 

To recap we covered attaching a widget to a window or frame, getting data from a widget, removing data from a widget, and populating our widget with data. There are many other things you can do to enhance the look and feel of an entry widget according to how you want your UI to look and feel but we will leave those things to another post. If there are any questions let me know in the comments below and if this post helped you please share this post.

 

2 Comments

  1. I have tried the Getting Data from Entry widget running it in PyCharm Edu. I cannot see the data in the entry box while typing until I click the get data button. Then if I select existing text and type a new entry same problem. There’s a cursor in the text box but it doesn’t respond to the keyboard entry. The program runs very slowly on my Mac. The get data button correctly returns what I typed. I tried running the script at the command line with same result. Am I missing something?

    • If you are just running the code outlined in this post it should not be slow at all, so the slowness may be something indicative of your machine.

      Troubleshooting your issues could you send me the version of python you are running. You can do this by issuing the command “python -V” on the command prompt.

      Also can you send me the exact code you are running, I have also sent you an email with this reply so feel free to reply to that email with your code.

Leave a Reply

Your email address will not be published.


*