पायथन में टिंकर एक जीयूआई पुस्तकालय है जिसका उपयोग विभिन्न जीयूआई प्रोग्रामिंग के लिए किया जा सकता है। ऐसे एप्लिकेशन डेस्कटॉप एप्लिकेशन बनाने के लिए उपयोगी होते हैं। इस लेख में हम GUI प्रोग्रामिंग के एक पहलू को देखेंगे जिसे बाइंडिंग फंक्शन कहा जाता है। यह घटनाओं को कार्यों और विधियों के लिए बाध्य करने के बारे में है ताकि जब घटना घटित हो तो उस विशिष्ट कार्य को निष्पादित किया जा सके।
बाइंडिंग कीबोर्ड इवेंट
नीचे दिए गए उदाहरण में हम कीबोर्ड से किसी भी कुंजी के प्रेस को एक फ़ंक्शन के साथ बांधते हैं जो निष्पादित हो जाता है। टिंकर जीयूआई विंडो खुलने के बाद, हम कीबोर्ड में कोई भी कुंजी दबा सकते हैं और हमें एक संदेश मिलता है कि कीबोर्ड दबाया गया है।
उदाहरण
from tkinter import *
# Press a buton in keyboard
def PressAnyKey(label):
value = label.char
print(value, ' A button is pressed')
base = Tk()
base.geometry('300x150')
base.bind('<Key>', lambda i : PressAnyKey(i))
mainloop()
आउटपुट
उपरोक्त कोड को चलाने से हमें निम्नलिखित परिणाम मिलते हैं -

बाध्यकारी माउस क्लिक इवेंट
नीचे दिए गए उदाहरण में हम देखते हैं कि टिंकर विंडो पर माउस क्लिक ईवेंट को फ़ंक्शन कॉल में कैसे बाँधें। नीचे दिए गए उदाहरण में हम ईवेंट को बाएं-बटन डबल क्लिक, राइट बटन क्लिक और स्क्रॉल-बटन क्लिक को टिंकर कैनवास में स्थिति प्रदर्शित करने के लिए कहते हैं जहां बटन क्लिक किए गए थे।
उदाहरण
from tkinter import *
from tkinter.ttk import *
# creates tkinter window or root window
base = Tk()
base.geometry('300x150')
# Press the scroll button in the mouse then function will be called
def scroll(label):
print('Scroll button clicked at x = % d, y = % d'%(label.x, label.y))
# Press the right button in the mouse then function will be called
def right_click(label):
print('right button clicked at x = % d, y = % d'%(label.x, label.y))
# Press the left button twice in the mouse then function will be called
def left_click(label):
print('Double clicked left button at x = % d, y = % d'%(label.x, label.y))
Function = Frame(base, height = 100, width = 200)
Function.bind('<Button-2>', scroll)
Function.bind('<Button-3>', right_click)
Function.bind('<Double 1>', left_click)
Function.pack()
mainloop() आउटपुट
उपरोक्त कोड को चलाने से हमें निम्नलिखित परिणाम मिलते हैं -
