How to take input in a text widget and display the text in tkinter?
Tkinter is a popular GUI (Graphical User Interface) package in Python that allows developers to create and design websites with ease. The text widget is a significant component of Tkinter. It allows users to enter text and exhibit it in a Graphical User Interface. In this article, we will learn how to take input in a text widget and display the text in Tkinter.
Requirements:
- Python 3.x (recommended to use the latest version)
- A Python IDE (Integrated Development Environment) or text editor.
- Tkinter installed on your computer (It is included by default in Python)
Creating a Text Widget:
Suppose we want to create a text widget. In that case, we can employ Tkinter’s Text widget method by building the widget and assigning it to a variable. Then we position it in the center of the main window, its size with the help of the place() method. Let’s write the code for the same:
#Importing functions from Tkinter
from tkinter import *
#Creating the main window
root = Tk()
#Creating the Text widget
T = Text(root, height = 2, width = 30)
#Positioning the widget in the middle of the main window
T.place(x=100,y=100)
#Setting the window size
root.geometry("400x400")
#Running the main loop
root.mainloop()
In this code, we have created a new Text widget named T, which we positioned in the middle of the primary window using the place() method. We set its height and width using the parameters offered in the Text() function.
Reading Input from the Text Widget:
To take input from the text widget, we have to use two elements that are get() and insert().
Using get() method:
The get() method is used to read the text inserted in the text widget. The syntax for using the get() method is as follows:
text = T.get("1.0", END)
print(text)
In this code snippet, we have employed the get() method to read the text entered in the text widget. The function will return the text from the first character, i.e., 1.0, to the END, which indicates the end of the entire text widget. The resulting text will be stored in the text variable that we can employ to display the value whenever required.
Let’s use this function in our previous code snippet to display the text in a message box on the button click:
#Importing messagebox functions from tkinter
from tkinter import messagebox
def get_text():
#Getting the text from the widget
text = T.get("1.0", END)
messagebox.showinfo("Information", text)
#Creating a button
btn = Button(root, text="Click Me!", command = get_text)
btn.place(x=150, y=150)
#Running the mainloop
root.mainloop()
In this example, we have used the get() method to read the text entered in the text box when the ‘Click Me!’ button is clicked. We store this text in the text variable and then used the showinfo() method to display this text in a message box.
Using insert() method:
The insert() method allows us to add text to the text widget programmatically. The syntax of the insert() method is as follows:
T.insert(END, "Some Text")
In this code snippet, the text ‘Some Text’ is added to the existing text in the Text widget from the END. The END keyword indicates the last position in the text widget. We can utilize this to show a preview of the entered text in a label or any other widget.
Let’s use this function in our previous code snippet to display a label having preview of the entered text:
def update_label():
#Getting the text from the widget
text = T.get("1.0", END)
#clearing the label space
lbl.config(text="")
#Adding the preview text to the label
lbl.insert(END, text)
#Creating a button
btn = Button(root, text="Preview", command = update_label)
btn.place(x=150, y=150)
#Creating the label
lbl = Text(root, height=2, width=30)
lbl.place(x=100, y=200)
#Running the mainloop
root.mainloop()
In this example, we have used the insert() method to insert the entered text into an existing label whenever the “Preview” button is clicked.
Conclusion:
In conclusion, the Text widget is an essential GUI element in Tkinter, and it is crucial to take input from it and display it whenever required. In this article, we have learnedhow to create a Text widget, read input from it using different methods (get() and insert()), and display the text in other widgets like message boxes, labels, etc. We hope this article has been helpful to you in your learning journey of Tkinter. Happy coding!