Last updated: Jun 9, 2023
Reading timeยท4 min
Entry
widget when Enter
is pressedTo get the value of an Entry widget in Tkinter:
ttk.Entry
class and store the result in a variable.ttk.Button
class to create a Button widget.entry.get()
method to get the value
of the Entry widget.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()
We used the Tk()
class to create a top-level widget and set its width to 600px
and its height to 300px.
root = Tk() root.geometry('600x300')
The next step is to use the ttk.Entry()
class to create an Entry
widget.
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.
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.
def get_entry_value(): value = entry.get() print(value) label.config(text=value)
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.
The last step is to create a Button
widget using the ttk.Button()
class.
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.
To be more precise, when the button is clicked:
entry.get()
method is used to get the value of the Entry
widget.Label
widget is updated to the current value of the Entry
widget.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.
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()
Notice that we used the int()
class to convert the value of the Entry
widget
to an integer.
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.
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 as
the Tkinter application loads.
For example, calling entry.get()
will return an empty string in the following
code sample.
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()) # ๐๏ธ ""
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).
Entry
widget when Enter
is pressedThe 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.
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)
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
.
You can learn more about the related topics by checking out the following tutorials: