Open In App

How To Print Entry Text Tkinter?

Last Updated : 20 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Tkinter is a popular GUI library in Python that allows developers to create desktop applications. One common task is retrieving and displaying the text entered by users. In this article, we'll go through the steps to create a simple Tkinter application that prints the text from an Entry widget in Python.

Steps to Print Tkinter Entry Text

To print the text entered in an Entry widget, you need to retrieve the text content and then display it, either in the console, another widget, or a message box. The basic steps include:

  1. Create an Entry Widget: Add an Entry widget to your Tkinter window.
  2. Create a Button: Add a button that, when clicked, will trigger the action to print the text.
  3. Retrieve the Text: Use the get() method of the Entry widget to retrieve the text.
  4. Print the Text: Print the text to the console or display it using a label or message box.

Printing Entry Text in Tkinter

Now let us see some of the ways by which we can print entry text in Tkinter using Python.

Using a Button Command

In this example, we will make the use of a Tkinter Button, to trigger the function to print the text entered by the user in the Entry widget.

We will create a simple Tkinter window application with an Entry widget and a Button. When the user enters some text in the Entry widget and clicks the Button, the Button's 'command' parameter calls the function which will retrieve the text from the Entry widget using entry.get() function and print it on the console.

Python
# import tkinter module
import tkinter as tk

# function to get text from entry widget
def print_entry_text():
    entry_text = entry.get()
    print(entry_text)

# main application window
root = tk.Tk()
root.title("Tkinter Entry Example")

# create entry widget
entry = tk.Entry(root, width=30)
entry.pack(pady=20)

# create button to display the content of entry widget
button = tk.Button(root, text="Print Text", 
                   command=print_entry_text)
button.pack(pady=10)

# run the application
root.mainloop()

Output:

Print Entry Text Tkinter using Button
Print Entry Text Tkinter using Button

Using Key Binding

In this example, we will make the use of a Tkinter Key Binding operation, to trigger the function to print the text entered by the user in the Entry widget.

We will create a simple Tkinter window application with an Entry widget. When the user enters some text in the Entry widget and presses the "Enter" key on the keyboard, the 'print_entry_text()' function gets called which will retrieve the text from the Entry widget using entry.get() function and print it on the console.

Python
# import tkinter module
import tkinter as tk

# function to get text from entry widget
def print_entry_text(event):
    entry_text = entry.get()
    print(entry_text)

# main application window
root = tk.Tk()
root.title("Tkinter Entry Example")

# create entry widget
entry = tk.Entry(root, width=30)
entry.pack(pady=20)

# key bindinf event
entry.bind("<Return>", print_entry_text)

# run the application
root.mainloop()

Output:

Print Entry Text Tkinter using Key Binding
Print Entry Text Tkinter using Key Binding

Displaying Text in a Label

In this example, we will make the use of a Tkinter Label and a Button, to trigger the function to print the text entered by the user in the Entry widget.

We will create a simple Tkinter window application with an Entry widget, a Button and a Label which will be empty at first. When the user enters some text in the Entry widget and clicks the Button, the Button's 'command' parameter calls the function which will retrieve the text from the Entry widget using entry.get() function. Here after getting the text in the entry widget, we will update the label created in the Tkinter window by using Tkinter config() function.

Python
# import tkinter module
import tkinter as tk

# function to get text from entry widget
def print_entry_text():
    entry_text = entry.get()
    label.config(text=entry_text)

# main application window
root = tk.Tk()
root.title("Tkinter Entry Example")

# create entry widget
entry = tk.Entry(root, width=30)
entry.pack(pady=20)

# create button to display the content of entry widget
button = tk.Button(root, text="Print Text", 
                   command=print_entry_text)
button.pack(pady=10)

# Create a Label widget to display the text
label = tk.Label(root, text="")
label.pack(pady=10)

# run the application
root.mainloop()

Output:

Print Entry Text Tkinter in a Label
Print Entry Text Tkinter in a Label

Conclusion

Printing the text from an Entry widget in Tkinter is a straightforward process. By understanding the Entry widget and using the get() method, you can easily retrieve and display user input in various ways.


Next Article
Article Tags :
Practice Tags :

Similar Reads