How to insert text at the beginning of the text box in Tkinter?


There are two types of Text editors that Tkinter supports -- the Entry widget and the Text widget. Tkinter text widgets are used to create the text editor where we can edit, add or delete the text whenever we need. We can create a text widget using the Text(parent) constructor. In order to insert a default text for the text widget, we can use the insert(INSERT, "text_to_insert") or insert("1.0", "text_to_insert") method after the text widget declaration.

Example

In this example, we will insert the text at the beginning of the text box.

#Import the required Libraries
from tkinter import *

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

#Set the geometry of tkinter frame
win.geometry("750x250")

#Create a Text Box
text= Text(win, width= 50, height= 30, background=
"gray71",foreground="#fff",font= ('Sans Serif', 13, 'italic bold'))

#Insert the text at the begining
text.insert(INSERT, "Write Something About Yourself")
text.pack(expand= 1, fill= BOTH)

win.mainloop()

Output

Run the above code to display the text box in the window.

In the given output, the default text is initially set to the beginning in the text box.

Updated on: 04-May-2021

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements