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

TkinterPythonServer Side ProgrammingProgramming

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.

raja
Published on 26-Mar-2021 10:28:48
Advertisements