Open In App

How to Get the Input From Tkinter Text Box?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Tkinter, the Text widget allows users to input multiple lines of text, making it perfect for scenarios where you need to collect large amounts of data, such as messages, notes or any other textual information. To retrieve the text that a user has entered into the Text widget, you can use the get() method. This method allows you to extract the content, either from the entire widget or from a specific range of text. In this article, we will cover how to retrieve the text entered in a Text widget using the get() method.

Syntax

get(start, [end])

Parameters:

  • start: The starting index of the required text in the Text widget.
  • end (optional): The ending index of the required text in the Text widget. If end is not provided, only the character at start is retrieved. A common end value is "end-1c" to get all text except the last newline.

Approach:

  • Create a Tkinter window.
  • Create a Text widget for multi-line input.
  • Create a Button widget that triggers text retrieval.
  • Implement a function to get text from the Text widget and display it using a Label.

Example: In this example, we will see a simple Tkinter application that allows users to input text into a multi-line textbox. When the user clicks the "Print" button, the inputted text will be displayed in a label.

Python
import tkinter as tk

root = tk.Tk()
root.title("TextBox Input")
root.geometry('400x200')

# Function to display input
def show_input():
    lbl.config(text=f"Input: {txt.get('1.0', 'end-1c')}")

# TextBox for input
txt = tk.Text(root, height=5, width=40)
txt.pack()

# Button to print input
btn = tk.Button(root, text="Print", command=show_input)
btn.pack()

# Label to display input
lbl = tk.Label(root, text="")
lbl.pack()

root.mainloop()

Output:

output
get the input

Explanation:

  • A Tkinter window is created with a Text widget for multi-line input.
  • When the "Print" button is clicked, the function show_input() is called.
  • This function retrieves the text from inputtxt using get("1.0", "end-1c").
  • The retrieved text is displayed inside a Label using config(text=...).

Examples

Example 1: In this example, we will create a Tkinter application that allows users to input text into a multi-line textbox. When the user clicks the "Save" button, the inputted text will be saved to a file named user_input.txt, and a label will confirm that the text was saved successfully.

Python
import tkinter as tk

# Function to save input text to a file
def save_to_file():
    text = txt.get("1.0", "end-1c")
    with open("user_input.txt", "w") as f:  
        f.write(text) 
    lbl.config(text="Text saved to user_input.txt")  

root = tk.Tk()
root.title("Save Input")
root.geometry("400x250")

# TextBox for input
txt = tk.Text(root, height=5, width=40)
txt.pack()

# Save Button
btn = tk.Button(root, text="Save", command=save_to_file)
btn.pack()

# Label to confirm save
lbl = tk.Label(root, text="")
lbl.pack()

root.mainloop()

Output:

Output
Save the input to text file

Explanation:

  • save_to_file() function retrieves text from the Text widget and writes it to a file (user_input.txt).
  • The with open(..., "w") statement ensures the file is opened in write mode and closed automatically after writing.
  • Once the text is saved, a confirmation message appears in the Label.

Example 2: In this example, we will demonstrate a simple Tkinter application that allows users to input text into a multi-line textbox. When the user clicks the "Clear" button, the text in the textbox will be cleared, and a label will confirm that the text has been cleared.

Python
import tkinter as tk

# Function to clear text and update label
def clearText():
    txt.delete("1.0", "end")
    lbl.config(text="Text Cleared")  

frame = tk.Tk()
frame.title("Clear TextBox")
frame.geometry("400x200")

# TextBox for input
txt = tk.Text(frame, height=5, width=40)
txt.pack()

# Clear Button
btn = tk.Button(frame, text="Clear", command=clearText)
btn.pack()

# Label to confirm clearing
lbl = tk.Label(frame, text="")
lbl.pack()

frame.mainloop()

Output:

Output
clear the input

Explanation:

  • clearText() function removes all text from the Text widget using delete("1.0", "end"), which clears everything from the first character to the end.
  • A label is updated to notify the user that the text has been cleared.

Article Tags :
Practice Tags :

Similar Reads