इस उदाहरण में, हम एक विंडो में एक गोलाकार बटन बनाएंगे जिसका उपयोग कई अन्य एप्लिकेशन जैसे फ़ॉर्म, गेम, डायलॉग बॉक्स आदि में किया जा सकता है।
टिंकर में गोल बटन बनाने का सबसे अच्छा तरीका बटन की वांछित छवियों का उपयोग करना और इसे फ्रेम में क्लिक करने योग्य बटन में बदलना है। PhotoImage() . का उपयोग करके यह वास्तव में संभव है फ़ंक्शन जो बटन की वांछित छवि को पकड़ लेता है।
तो, निम्नलिखित चरण वांछित छवि को एक बटन बनाते हैं,
-
सबसे पहले, हम एक डमी बटन बनाएंगे जिसका उपयोग छवि को क्लिक करने योग्य बनाने के लिए किया जा सकता है।
-
PhotoImage(file) फ़ंक्शन का उपयोग करके स्रोत से छवि प्राप्त करें।
-
छवि फ़ाइल को बटन फ़ंक्शन में मान के रूप में पास करें
-
सीमा-चौड़ाई =0 निकालें।
-
अब, हमने बटन को गोल कर दिया है।
इस उदाहरण के लिए हम इस छवि का उपयोग करेंगे और इसे क्लिक करने योग्य बनाएंगे।
#Import all the necessary libraries
from tkinter import *
#Define the tkinter instance
win= Toplevel()
win.title("Rounded Button")
#Define the size of the tkinter frame
win.geometry("700x300")
#Define the working of the button
def my_command():
text.config(text= "You have clicked Me...")
#Import the image using PhotoImage function
click_btn= PhotoImage(file='clickme.png')
#Let us create a label for button event
img_label= Label(image=click_btn)
#Let us create a dummy button and pass the image
button= Button(win, image=click_btn,command= my_command,
borderwidth=0)
button.pack(pady=30)
text= Label(win, text= "")
text.pack(pady=30)
win.mainloop() आउटपुट
उपरोक्त कोड को चलाने से निम्न आउटपुट प्राप्त होगा -
