How to set the text/value/content of an 'Entry' widget using a button in Tkinter?

Tkinter Entry widget is used to display a single line text. Using tkinter Entry widget, we can set its value or content by triggering a button. It has mainly two types of operation: insert and delete.

Using the Tkinter Button widget, we will set the content of the Entry widget.


#Import the required libraries
from tkinter import *

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

#Define a function to change the value
def change_text(txt):

#Set the geometry of frame

#Create an Entry Widget
text= Entry(win)

#Create a button to change/set the content
btn= Button(win,text="Set", command=lambda:change_text("My New Text"))


Running the code will display a window containing a button that will set the value or text of the entry widget.

Now click on the “Set” button to set a new value for Entry Widget.