How do I print and have user input in a text box in Tkinter?

Tkinter's Text widget allows you to create multi-line text input areas and display information. You can retrieve user input using the get() method and display content using insert(). Here's how to create an interactive text box application.

Basic Text Widget Example

This example creates a text box where users can type, and clicking "Print" displays the content in a label ?

# Import the required library
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win = Tk()

# Set the geometry
win.geometry("700x350")
win.title("Text Widget Example")

def get_input():
    # Get text from position 1.0 to end, excluding the last newline
    user_text = text.get(1.0, "end-1c")
    label.config(text="You entered: " + user_text)

# Add a text widget
text = Text(win, width=80, height=10, wrap=WORD)
text.insert(END, "Type your message here...")
text.pack(pady=10)

# Create a button to get the text input
button = ttk.Button(win, text="Print Text", command=get_input)
button.pack(pady=5)

# Create a Label widget to display output
label = Label(win, text="", font=('Arial', 12), wraplength=600)
label.pack(pady=10)

win.mainloop()

Advanced Text Widget with Multiple Operations

This example shows more text widget operations including clearing, saving, and formatting ?

from tkinter import *
from tkinter import ttk, messagebox

win = Tk()
win.geometry("800x500")
win.title("Advanced Text Widget")

def display_text():
    content = text.get(1.0, END)
    output_text.delete(1.0, END)
    output_text.insert(1.0, "Output:\n" + content)

def clear_text():
    text.delete(1.0, END)
    output_text.delete(1.0, END)

def count_words():
    content = text.get(1.0, "end-1c")
    word_count = len(content.split())
    char_count = len(content)
    messagebox.showinfo("Statistics", f"Words: {word_count}\nCharacters: {char_count}")

# Input text widget
Label(win, text="Input Text:", font=('Arial', 12, 'bold')).pack(anchor=W, padx=10, pady=5)
text = Text(win, width=90, height=8, wrap=WORD)
text.pack(padx=10, pady=5)

# Buttons frame
button_frame = Frame(win)
button_frame.pack(pady=10)

ttk.Button(button_frame, text="Display Text", command=display_text).pack(side=LEFT, padx=5)
ttk.Button(button_frame, text="Clear All", command=clear_text).pack(side=LEFT, padx=5)
ttk.Button(button_frame, text="Word Count", command=count_words).pack(side=LEFT, padx=5)

# Output text widget
Label(win, text="Output Text:", font=('Arial', 12, 'bold')).pack(anchor=W, padx=10, pady=(20,5))
output_text = Text(win, width=90, height=8, wrap=WORD, state=NORMAL)
output_text.pack(padx=10, pady=5)

win.mainloop()

Key Methods and Properties

Method/Property Purpose Example
get(start, end) Retrieve text content text.get(1.0, END)
insert(index, text) Insert text at position text.insert(END, "Hello")
delete(start, end) Delete text range text.delete(1.0, END)
wrap=WORD Word wrapping Wraps at word boundaries

Important Index Positions

  • 1.0 - Beginning of text (line 1, character 0)
  • END - End of all text including final newline
  • "end-1c" - End of text excluding final newline
  • INSERT - Current cursor position

Conclusion

Use get(1.0, "end-1c") to retrieve user input from Text widgets. The Text widget provides powerful text editing capabilities with methods for insertion, deletion, and formatting in Tkinter applications.

Updated on: 2026-03-26T00:12:54+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements