How to get the input from the Tkinter Text Widget?

TkinterPythonServer Side ProgrammingProgramming

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.

raja
Published on 26-Mar-2021 10:49:27
Advertisements