
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to pass an argument to the event handler in Tkinter?
In most situations, the callback functions can refer to as an Instance Method. An instance method accesses all its members and performs operations with them without specifying any arguments.
Let's consider a case where more than one component is defined and we want to handle some events with those components. To run multiple events, we prefer to pass multiple arguments in event handlers.
Example
In this example, we have created multiple button widgets in a frame, and we will handle various events by passing the name of the widget as arguments. Once a Button will be clicked, it will update the Label widget and so on.
#Import the Tkinter library from tkinter import * from tkinter import ttk from tkinter import filedialog #Create an instance of Tkinter frame win= Tk() #Define the geometry win.geometry("750x250") #Define Event handlers for different Operations def event_low(button1): label.config(text="This is Lower Value") def event_mid(button2): label.config(text="This is Medium Value") def event_high(button3): label.config(text="This is Highest value") #Create a Label label= Label(win, text="",font=('Helvetica 15 underline')) label.pack() #Create a frame frame= Frame(win) #Create Buttons in the frame button1= ttk.Button(frame, text="Low", command=lambda:event_low(button1)) button1.pack(pady=10) button2= ttk.Button(frame, text="Medium",command= lambda:event_mid(button2)) button2.pack(pady=10) button3= ttk.Button(frame, text="High",command= lambda:event_high(button3)) button3.pack(pady=10) frame.pack() win.mainloop()
Output
Running the above code will display a window that contains Buttons Low, Medium, and High. When we click a button, it will show some label text on the window.
- Related Articles
- How do I pass an event handler to a component in ReactJS?
- How to pass argument to an Exception in Python?
- How to add an event handler to the specified element in JavaScript?\n
- How to add an event handler to an element in JavaScript HTML DOM?
- How to pass an entire structure as an argument to function in C?
- How to pass the address of structure as an argument to function in C?
- How to pass entire structure as an argument to function in C language?
- How to pass the address of structure as an argument to function in C language?
- How to pass entire array as an argument to a function in C language?
- How to pass individual elements in an array as argument to function in C language?
- How do I add a simple onClick event handler to an HTML5 canvas element?
- How to pass a dictionary as argument in Python function?
- How to pass Python function as a function argument?
- How to pass arguments to a Button command in Tkinter?
- Can we pass objects as an argument in Java?
