- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I pass arguments to Tkinter button's callback command?
Tkinter Buttons are used for handling certain operations in the application. In order to handle such events, we generally pass the defined function name as the value in the callback command. For a particular event, we can also pass the argument to the function in the button’s command.
There are two ways to pass the argument to the tkinter button command −
- Using Lambda or anonymous function
- Using Partials
In this example, we will create a simple application that will contain a text label and a button to change the value of label text. We will pass the label as the argument in the button command by using lambda function.
#Import necessary Library from tkinter import * from tkinter import ttk #Create an instance of tkinter window win= Tk() #Set the geometry of tkinter window win.geometry("750x250") #Define the function to change the value in label widget def change_text(label): label.configure(text= "Hey, I am Label-2", background="gray91") #Create a Label label = Label(win, text= "Hey, I am Label-1", font= ('Helvetica 15 underline'), background="gray76") label.pack(pady=20) #Create a button btn= ttk.Button(win, text= "Change", command= lambda:change_text(label)) btn.pack(pady=10) win.mainloop()
Running the above code will display a window which contains a text label and a button to change the value of the label.
Now click on “Change” button to change the value of the label widget.
- How to pass arguments to a Button command in Tkinter?
- Passing arguments to a Tkinter button command
- How can I pass a parameter to a setTimeout() callback?
- How to pass arguments in Invoke-Command in PowerShell?
- How can I vary a shape's alpha with Tkinter?
- How can I pass parameters to on_key in fig.canvas.mpl_connect('key_press_event',on_key)?
- MySQL's DESCRIBE command?
- How to clean one's belly button?
- How can I override the 'Home' button in my Android App?
- How can I identify when a Button is released in Tkinter?
- How to delete all children's elements using Python's Tkinter?
- Change command Method for Tkinter Button in Python
- How can I play a sound when a Tkinter button is pushed?
- How to Delete Tkinter Text Box's Contents?