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

Tkinter में On/Off Toggle Button Switch

Tkinter एप्लिकेशन के लिए आवश्यक विभिन्न प्रकार के widgets जोड़ने की सुविधाएं प्रदान करता है। इनमें से कुछ widgets हैं: Button widget, Entry Widget, Text Box, Slider, आदि। इस लेख में, हम देखेंगे कि कैसे एक ऐसा एप्लिकेशन बनाया जाए जिसमें एक button हो जो on या off हो सकता है।

इस उदाहरण में, हम प्रदर्शन के लिए इन दो buttons का उपयोग करेंगे:

  • Switch On

  • Switch Off

उदाहरण

# Import tkinter in the notebook
from tkinter import *

# Create an instance of window of frame
win = Tk()

# set Title
win.title('On/Off Demonstration')

# Set the Geometry
win.geometry("600x400")
win.resizable(0, 0)

# Create a variable to turn on the button initially
is_on = True

# Create Label to display the message
label = Label(win, text="Night Mode is On", bg="white", fg="black", font=("Poppins bold", 22))
label.pack(pady=20)

# Define our switch function
def button_mode():
    global is_on
    # Determine it is on or off
    if is_on:
        on_.config(image=off)
        label.config(text="Day Mode is On", bg="white", fg="black")
        is_on = False
    else:
        on_.config(image=on)
        label.config(text="Night Mode is On", fg="black")
        is_on = True

# Define Our Images
on = PhotoImage(file="on.png")
off = PhotoImage(file="off.png")

# Create A Button
on_ = Button(win, image=on, bd=0, command=button_mode)
on_.pack(pady=50)

# Keep Running the window
win.mainloop()

आउटपुट

उपरोक्त code चलाने से on/off mode operate करने के लिए एक Button बनेगा।

Tkinter में On/Off Toggle Button Switch

यदि आप button पर क्लिक करते हैं, तो यह निम्नानुसार बदल जाएगा:

Tkinter में On/Off Toggle Button Switch

  1. Python में tkinter में बटन बनाना Python में tkinter में बटन बनाना

    Python के लिए एक लाइब्रेरी के रूप में Tkinter, tkinter कैनवास पर बटन बनाने के कई तरीके प्रदान करता है। इस लेख में हम देखेंगे कि हम सामान्य tkinter मॉड्यूल के साथ-साथ थीम वाले tkinter मॉड्यूल का उपयोग किए बिना tkinter बटन कैसे बना सकते हैं। tkinter का उपयोग करना नीचे दिए गए प्रोग्राम में हम पहले एक क

  1. Python tkinter बटन में स्टाइल जोड़ना Python tkinter बटन में स्टाइल जोड़ना

    Tkinter का Python पर आधारित GUI प्रोग्राम बनाने के लिए बहुत अच्छा समर्थन है। यह कैनवास पर इसके फ़ॉन्ट, आकार, रंग आदि के आधार पर बटन को स्टाइल करने के विभिन्न तरीके प्रदान करता है। इस लेख में हम देखेंगे कि कैनवास पर विशिष्ट बटनों या सामान्य रूप से सभी बटनों पर स्टाइल कैसे लागू करें। विशिष्ट बटनों पर

  1. Python Tkinter बटन पर छवि जोड़ें Python Tkinter बटन पर छवि जोड़ें

    Tkinter, जो Python प्रोग्रामिंग के लिए GUI लाइब्रेरी है, में GUI बटनों पर छवियां जोड़ने की सुविधा है। यह उपयोगकर्ताओं के लिए GUI में टेक्स्ट के बजाय प्रतीकों को याद रखने के लिए उपयोगी है। नीचे दिए गए Tkinter प्रोग्राम में हम दिखाते हैं कि हम GUI बटन पर छवि कैसे जोड़ सकते हैं। imageKT मॉड्यूल से phot