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


We can use the Tkinter text widget to insert text, display information, and get the output from the text widget. To get the user input in a text widget, we've to use the get() method. Let's take an example to see how it works.

Example

# 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")

def get_input():
   label.config(text=""+text.get(1.0, "end-1c"))

# Add a text widget
text=Text(win, width=80, height=15)
text.insert(END, "")
text.pack()

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

# Create a Label widget
label=Label(win, text="", font=('Calibri 15'))
label.pack()

win.mainloop()

Output

Running the above code will display a window containing a text widget. Type something in the text widget and click the "Print" button to display the output.

Updated on: 05-Aug-2021

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements