Passing arguments to a Tkinter button command

TkinterPythonGUI-Programming

The Button widget in Tkinter is generally used for pushing an event defined in an application. We can bind the events with buttons that allow them to execute and run whenever an action is triggered by the user.

However, sharing the data and variables outside the function and events seems difficult sometimes. With the Button widget, we can pass arguments and data that allows the user to share and execute the event.

In general, passing the arguments to a button widget allows the event to pick the arguments and use them further in the program.

Example

# Import the required library
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

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

# Set the geometry
win.geometry("700x250")

# Define a function to update the entry widget
def update_name(name):
   entry.insert(END, ""+str(name))

# Create an entry widget
entry=Entry(win, width=35, font=('Calibri 15'))
entry.pack()

b=ttk.Button(win, text="Insert", command=lambda:update_name("Tutorialspoint"))
b.pack(pady=30)

win.mainloop()

Output

Running the above code will display a window with an Entry widget and a button to insert text in it.

Click the button "Insert" to add text in the Entry widget.

raja
Published on 05-Aug-2021 14:11:28
Advertisements