How to get the value of a button in the Entry widget using Tkinter?


Buttons are a very useful widget in any Tkinter application. We can get the value of any button in the Entry widget by defining the function which inserts the value in the Entry widget. To get the value, we have to first define buttons having command for adding the specific value to be displayed on the Entry widget.

To update the Entry widget, we can delete the previous value using delete(0, END) method.

Example

# Import the required libraries
from tkinter import *
from tkinter import ttk

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

# Set the size of the window
win.geometry("700x350")

def on_click(text):
   entry.delete(0, END)
   entry.insert(0,text)

# Add an Entry widget
entry=Entry(win, width= 25)
entry.pack()

# Add Buttons in the window
b1=ttk.Button(win, text= "A", command=lambda:on_click("A"))
b1.pack()

b2=ttk.Button(win, text= "B", command=lambda: on_click("B"))
b2.pack()

b3=ttk.Button(win, text= "C", command=lambda: on_click("C"))
b3.pack()

win.mainloop()

Output

Running the above code will display a window that contains a number of buttons in it. When we click a button, it will display its value in the Entry field.

 

Updated on: 18-Jun-2021

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements