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.

Example

#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):
   text.delete(0,END)
   text.insert(0,txt)

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

#Create an Entry Widget
text= Entry(win)
text.pack()

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

Output

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.

Updated on: 26-Mar-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements