HOWTO · Tkinter

How to Get the Input From Tkinter Text Box

This article explores how to get input from a Tkinter Text Box using the get method. Learn to specify start and end indices for precise text retrieval, implement validation, and enhance your Python applications with user-friendly interfaces. Discover practical code examples and detailed explanations to effectively capture user input in your Tkinter projects.

On this page

To read a Tkinter Text widget, call its get method with a starting index and an ending index. For the full contents, use text_box.get("1.0", "end-1c"): 1.0 marks the start, and end-1c leaves out the widget’s final newline.

Retrieve Text with the Tkinter Text Widget

This complete window lets a user enter several lines and retrieve them with a button. The callback keeps the returned string in user_input; replace the print call with the processing your application needs.

import tkinter as tk

def get_input():
    user_input = text_box.get("1.0", "end-1c")
    print("User Input:", user_input)

root = tk.Tk()
root.title("Read Text Input")

text_box = tk.Text(root, height=8, width=40)
text_box.pack(padx=8, pady=8)

get_button = tk.Button(root, text="Get Input", command=get_input)
get_button.pack(pady=(0, 8))

root.mainloop()

This GUI example is unexecuted in the supplied runtime evidence; the explanation follows Tkinter’s index semantics.

The first index is inclusive and the ending index marks where retrieval stops. get also accepts a single index; without an ending index, it returns the character at that position rather than the whole widget.

Choose Start and End Indices

Tkinter text indices use a line.column form. 1.0 means the first character on the first line, while 2.0 means the start of the second line. Therefore, text_box.get("1.0", "2.0") selects the first line, including its line break when present. Choose a later line and column to read a smaller range. The ending position is not included in the returned text.

Exclude the Trailing Newline

A Text widget always keeps a newline at the end, including when the user has typed no text. Reading through "end" includes that final newline. Use "end-1c" as the ending index to omit just that final character; the equivalent Tkinter expression is tk.END + "-1c". This distinction matters when comparing the retrieved value, saving it, or passing it to code that should not receive an extra line break.

Tkinter Text widget input retrieved with the get method

Partial text retrieved from a Tkinter Text widget with the get method

Validate Retrieved Input

When the application requires meaningful content, check a trimmed view of the string: if not user_input.strip():. This treats whitespace-only input as blank. Keep user_input unchanged if the original spacing or line breaks matter; use the trimmed value only for the check unless your application intentionally wants to normalize the user’s text. Show feedback in the interface when the check fails, and continue processing the original value when it passes.