How to get the value of an Entry widget in Tkinter

avatar
Borislav Hadzhiev

Last updated: Jun 9, 2023
4 min

banner

# Table of Contents

  1. How to get the value of an Entry widget in Tkinter
  2. The value of the Entry widget is always going to be of type string
  3. Make sure you aren't trying to get the value of an Entry widget immediately
  4. Getting the value of an Entry widget when Enter is pressed

# How to get the value of an Entry widget in Tkinter

To get the value of an Entry widget in Tkinter:

  1. Instantiate the ttk.Entry class and store the result in a variable.
  2. Instantiate the ttk.Button class to create a Button widget.
  3. When the button gets clicked, use the entry.get() method to get the value of the Entry widget.
main.py
from tkinter import * from tkinter import ttk root = Tk() root.geometry('600x300') entry = ttk.Entry(root) entry.pack(pady=50) label = ttk.Label(root, text='') label.pack(pady=20) def get_entry_value(): value = entry.get() print(value) label.config(text=value) button = ttk.Button(root, text="Get Entry Value", command=get_entry_value) button.pack(pady=25) root.mainloop()

get value of entry widget in tkinter

The code for this article is available on GitHub

We used the Tk() class to create a top-level widget and set its width to 600px and its height to 300px.

main.py
root = Tk() root.geometry('600x300')

The next step is to use the ttk.Entry() class to create an Entry widget.

main.py
entry = ttk.Entry(root) entry.pack(pady=50)

After creating the Entry widget, we pack it in the parent widget with a vertical padding of 50px.

The next step is to create a Label widget using the ttk.Label() class.

main.py
label = ttk.Label(root, text='') label.pack(pady=20)

Initially, we set the text of the label to an empty string because the value of the Entry widget is empty by default.

The get_entry_value() function prints the value of the Entry widget and sets the text of the label to the Entry widget's current value.

main.py
def get_entry_value(): value = entry.get() print(value) label.config(text=value)
The code for this article is available on GitHub

The entry.get() method returns the text of the Entry widget.

Notice that the value of the entry widget is also printed to your terminal.

value of entry widget printed to terminal

The last step is to create a Button widget using the ttk.Button() class.

main.py
button = ttk.Button( root, text="Get Entry Value", command=get_entry_value ) button.pack(pady=25)

We set the text of the button to "Get Entry Value".

The command keyword argument should be set to a function.

The function gets invoked every time the Button widget is clicked.

As shown in the short clip, every time the button is clicked, the value of the Entry widget is displayed.

get value of entry widget in tkinter

To be more precise, when the button is clicked:

  1. The entry.get() method is used to get the value of the Entry widget.
  2. The text of the Label widget is updated to the current value of the Entry widget.

# The value of the Entry widget is always going to be of type string

Note that the value of the Entry widget is always going to be of type str, even if the user enters an integer.

You can use the int() class if you need to convert the value of the Entry widget to an integer and the float() class to convert it to a floating-point number.

main.py
from tkinter import * from tkinter import ttk root = Tk() root.geometry('600x300') entry = ttk.Entry(root) entry.pack(pady=50) label = ttk.Label(root, text='') label.pack(pady=20) def calculate_salary(): # ๐Ÿ‘‡๏ธ Converting the Entry value to an integer value = int(entry.get()) print(type(value)) label.config(text=f'Your updated salary is {value + 100}') button = ttk.Button(root, text="Calculate Salary", command=calculate_salary) button.pack(pady=25) ttk.Label(root, text='bobbyhadz.com').pack(pady=10) root.mainloop()

get value of entry widget as integer

The code for this article is available on GitHub

Notice that we used the int() class to convert the value of the Entry widget to an integer.

main.py
def calculate_salary(): # ๐Ÿ‘‡๏ธ Converting the Entry value to an integer value = int(entry.get()) print(type(value)) label.config(text=f'Your updated salary is {value + 100}')

We then used a formatted string literal to add 100 to the supplied value and update the Label's text.

The curly braces syntax {} in the f-string enables us to evaluate an expression or interpolate a variable.

If you need to convert the value of the Entry widget to a floating-point number, use the float() class instead.

main.py
def calculate_salary(): # ๐Ÿ‘‡๏ธ Converting the Entry value to a float value = float(entry.get()) print(type(value)) label.config(text=f'Your updated salary is {value + 100}')

# Make sure you aren't trying to get the value of an Entry widget immediately

Make sure you aren't trying to get the value of an Entry widget immediately as the Tkinter application loads.

For example, calling entry.get() will return an empty string in the following code sample.

main.py
from tkinter import * from tkinter import ttk root = Tk() root.geometry('600x300') entry = ttk.Entry(root) entry.pack(pady=50) print('value is: ', entry.get()) # ๐Ÿ‘‰๏ธ ""
The code for this article is available on GitHub

This is because the value of the Entry widget is an empty string and the code runs immediately after Tkinter starts (before the user has entered a value).

# Getting the value of an Entry widget when Enter is pressed

The previous subheadings showed how to get the value of an Entry widget when a button is clicked.

However, you can also get the value of an Entry widget when the Enter (Return) key is pressed.

main.py
from tkinter import * from tkinter import ttk root = Tk() root.geometry('600x300') label = ttk.Label(root, text='') label.pack(pady=25) def handle_change(event): value = event.widget.get() print('Enter pressed', value) label.config(text=value) entry = ttk.Entry(root) entry.pack(pady=50) entry.bind("<Return>", handle_change)

get value of entry widget when enter key pressed

The code for this article is available on GitHub

We used the bind() method to bind the <Return> (Enter) key to the handle_change function.

The handle_change function uses the event.widget.get() method to get the value of the Entry widget from the event object.

The last step is to update the text of the Label widget to display the text of the Entry.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev