How to get the input from the Tkinter Text Widget?


In tkinter, we can create text widgets using Text attributes using packages. However, while creating a GUI application, sometimes we need to capture the input from a text widget.

We can get the input from the user in a text widget using the .get() method. We need to specify the input range which will be initially from 1.0 to END that shows the characters starting and ending till the END.

Example

#Import tkinter library
from tkinter import *

#Create an instance of tkinter window or frame
win=Tk()
win.geometry("700x300")

def get_input():
   value=my_text_box.get("1.0","end-1c")
   print(value)

#Creating a text box widget
my_text_box=Text(win, height=5, width=40)
my_text_box.pack()

#Create a button for Comment
comment= Button(win, height=5, width=10, text="Comment", command=lambda: get_input())

#command=get_input() will wait for the key to press and displays the entered text
comment.pack()

win.mainloop()

Output

Running the above code will display a textbox which will accept the input from the user and print the output on the console.

Updated on: 13-Jul-2021

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements