- 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 to pass arguments to a Button command in Tkinter?
Let us assume that we are working with a tkinter application such that there are some buttons which need to pull out some window or event. In order to make the button fully functional, we can pass some arguments as the command value.
The Command is a Button attribute which takes the function name as the value. The function defines the working of a particular event.
Let us first create a button and add some events by passing arguments to its command attribute.
In this example, we will create a window and a button that will close the window instantly.
#Importing the required library from tkinter import * #Create an instance of tkinter frame or window win= Tk() #Set the title win.title("Button Command Example") #Set the geometry win.geometry("600x300") #Create a label for the window Label(win, text= "Example", font= ('Times New Roman bold', 20)).pack(pady=20) #Defining a function def close_event(): win.destroy() #Create a button and pass arguments in command as a function name my_button= Button(win, text= "Close", font=('Helvetica bold', 20), borderwidth=2, command= close_event) my_button.pack(pady=20) win.mainloop()
By running the above code, we can pass the function as the argument to a Button command.
Click the "Close" button and it will close the window.
- Passing arguments to a Tkinter button command
- How can I pass arguments to Tkinter button's callback command?
- How to pass arguments in Invoke-Command in PowerShell?
- How to pass arguments to animation.FuncAnimation() in Matplotlib?
- Change command Method for Tkinter Button in Python
- How to pass arguments by reference in a Python function?
- How to Parse Command Line Arguments in C++?
- How to add command line arguments in Python?
- How to create a Tkinter toggle button?
- How to pass arguments by value in Python function?
- How to pass arguments by reference in Python function?
- How to Disable / Enable a Button in TKinter?
- How to update a Button widget in Tkinter?