इस उदाहरण में, हम एक window में rounded button बनाएंगे जिसका उपयोग forms, games, dialogue boxes आदि जैसे कई अन्य एप्लिकेशन में किया जा सकता है।
Tkinter में rounded buttons बनाने का सबसे अच्छा तरीका buttons की desired images का उपयोग करना और इसे frame में clickable button में बदलना है। यह PhotoImage() फंक्शन का उपयोग करके वास्तव में संभव है जो button की desired image को grab करता है।
तो, निम्न steps desired image को button बनाते हैं:
पहले, हम एक dummy button बनाएंगे जिसका उपयोग image को clickable बनाने के लिए किया जा सकता है।
PhotoImage(file) फंक्शन का उपयोग करके source से image grab करें।
Button फंक्शन में value के रूप में image file pass करें।
borderwidth=0 remove करें।
अब, हमें rounded button मिल गया।
इस उदाहरण के लिए हम इस image का उपयोग करेंगे और इसे clickable बनाएंगे।
# 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()
आउटपुट
उपरोक्त code चलाने से निम्न output produce होगा:
