Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Tkinter में Image को Button के रूप में कैसे उपयोग करें?

इस उदाहरण में, हम एक 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 होगा:

Tkinter में Image को Button के रूप में कैसे उपयोग करें?

  1. Tkinter Python में Thread का उपयोग कैसे करें? Tkinter Python में Thread का उपयोग कैसे करें?

    Tkinter के साथ, हम Threading का उपयोग करके एक समय में multiple functions call कर सकते हैं। यह एप्लिकेशन में कुछ functions के asynchronous execution को प्रदान करता है। Python में thread का उपयोग करने के लिए, हम threading नामक module import कर सकते हैं और इसकी Thread class को subclass कर सकते हैं। अपन

  1. Tkinter में Images को Backgrounds के रूप में कैसे उपयोग करें? Tkinter में Images को Backgrounds के रूप में कैसे उपयोग करें?

    यदि हम Tkinter frame का instance बनाएंगे और window display करेंगे जबकि इसे चलाते रहेंगे, तो यह default output canvas दिखाएगा। हालांकि, हम PhotoImage methods और Canvas methods का उपयोग करके Tkinter canvas के अंदर एक image को background के रूप में जोड़ सकते हैं। चूंकि Tkinter में image support GIF, PN

  1. Tkinter में Button में Bitmap Images का उपयोग कैसे करें? Tkinter में Button में Bitmap Images का उपयोग कैसे करें?

    Tkinter में, हम images का उपयोग करके buttons बना और customize कर सकते हैं। इन images को Python PhotoImage(file) फंक्शन का उपयोग करके upload किया जा सकता है। हालांकि, PhotoImage() केवल कुछ image types जैसे PNG, PPM, और GIF support करता है। इसके अलावा, हम BitMap images का उपयोग करके भी buttons बना सकत