Tkinter Entry insert() Method
When designing GUI programs, it is often necessary to create the input text by default in the text box where the Entry is created. You can use the insert(index,s) method to insert a string in the Entry control of the Widget, where s is the inserted string and the string will be inserted at the index position. You can use this method to create the default text for the text box when designing your application, and it is usually placed after the text box is created by the Enter( ) method.
Tkinter Entry insert() Method Example#1
Create a default text of Kevin
for the program’s Account text box and pwd
for the Password text box.
from tkinter import *
def printInfo():
print("Account:%s\nPassword: %s" %(accountE.get(),pwdE.get()))
root = Tk()
root.title("apidemos.com")
msg = "Welcome to apidemos.com"
myPic = PhotoImage(file="apidemos.png")
logo = Label(root,image=myPic,text=msg,compound=BOTTOM)
accountL = Label(root,text="Account")
accountL.grid(row=1)
pwdL = Label(root,text="Password")
pwdL.grid(row=2)
logo.grid(row=0,column=0,columnspan=2,pady=10,padx=10)
accountE = Entry(root)
pwdE = Entry(root,show="*")
accountE.insert(0,"Kevin")
pwdE.insert(0,"pwd")
accountE.grid(row=1,column=1)
pwdE.grid(row=2,column=1,pady=10)
# The following Login and Quit buttons are created
loginbtn = Button(root,text="Login",command=printInfo)
loginbtn.grid(row=3,column=0,sticky=W,pady=5)
quitbtn = Button(root,text="Quit",command=root.quit)
quitbtn.grid(row=3,column=1,sticky=W,pady=5)
root.mainloop()
Output: