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.

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.

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

#!/usr/bin/python3

fromtkinterimportEntry

fromtkinterimportTk

fromtkinterimportButton

defget_entry_data():

data=entry_widget.get()

print(str(data))

root=Tk()

root.geometry('400x400')

rows=0

whilerows<10:

root.rowconfigure(rows,weight=1)

root.columnconfigure(rows,weight=1)

rows+=1

entry_widget=Entry(root)

entry_widget.grid(row=4,column=4)

action_button=Button(root)

action_button.configure(text='Get Data',command=get_entry_data)

action_button.grid(row=5,column=4)

root.mainloop()

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.

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

#!/usr/bin/python3

fromtkinterimportEntry

fromtkinterimportTk

fromtkinterimportButton

fromtkinterimportEND

defget_entry_data():

data=entry_widget.get()

# deletes all existing data from our entry widget

entry_widget.delete(0,END)

print(str(data))

root=Tk()

root.geometry('400x400')

rows=0

whilerows<10:

root.rowconfigure(rows,weight=1)

root.columnconfigure(rows,weight=1)

rows+=1

entry_widget=Entry(root)

entry_widget.grid(row=4,column=4)

action_button=Button(root)

action_button.configure(text='Get Data',command=get_entry_data)

action_button.grid(row=5,column=4)

root.mainloop()

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.

Python

1

2

3

4

5

6

7

defget_entry_data():

data=entry_widget.get()

# deletes all existing data from our entry widget

entry_widget.delete(0,1)

print(str(data))

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.

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

#!/usr/bin/python3

fromtkinterimportEntry

fromtkinterimportTk

fromtkinterimportButton

fromtkinterimportEND

defget_entry_data():

data=entry_widget.get()

# deletes all existing data from our entry widget

entry_widget.delete(0,END)

print(str(data))

root=Tk()

root.geometry('400x400')

rows=0

whilerows<10:

root.rowconfigure(rows,weight=1)

root.columnconfigure(rows,weight=1)

rows+=1

entry_widget=Entry(root)

entry_widget.grid(row=4,column=4)

entry_widget.insert(0,'Enter Text Here')

action_button=Button(root)

action_button.configure(text='Get Data',command=get_entry_data)

action_button.grid(row=5,column=4)

root.mainloop()

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

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?

So Last week it was made public a vulnerability in the AirMAX firmware that allowed for remote command execution affected some of their AirMAX products. I came across it via This Link on Spiceworks, now at first glance [...]