ज्यादातर स्थितियों में, कॉलबैक फ़ंक्शन को इंस्टेंस विधि के रूप में संदर्भित किया जा सकता है। एक इंस्टेंस विधि अपने सभी सदस्यों तक पहुँचती है और बिना किसी तर्क को निर्दिष्ट किए उनके साथ संचालन करती है।
आइए एक मामले पर विचार करें जहां एक से अधिक घटक परिभाषित हैं और हम उन घटकों के साथ कुछ घटनाओं को संभालना चाहते हैं। कई इवेंट चलाने के लिए, हम इवेंट हैंडलर में कई तर्क देना पसंद करते हैं।
उदाहरण
इस उदाहरण में, हमने एक फ्रेम में कई बटन विजेट बनाए हैं, और हम विजेट के नाम को तर्क के रूप में पास करके विभिन्न घटनाओं को संभालेंगे। एक बार बटन पर क्लिक करने के बाद, यह लेबल विजेट आदि को अपडेट कर देगा।
#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() आउटपुट
उपरोक्त कोड को चलाने से एक विंडो प्रदर्शित होगी जिसमें बटन निम्न, मध्यम और उच्च होंगे। जब हम एक बटन पर क्लिक करते हैं, तो यह विंडो पर कुछ लेबल टेक्स्ट दिखाएगा।
