Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_custom_toggle_widget.py
- from tkinter import *
- from PIL import ImageTk, Image
- import base64
- root = Tk()
- root.title('Tk Toggle Switch')
- root.geometry("400x150")
- # Define Our Images
- b64_on = '''
- iVBORw0KGgoAAAANSUhEUgAAAEAAAAAgCAYAAACinX6EAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
- YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAA4sSURBVGhDzVlrjFXVGV3nnHvnzsDMAAPyEBxmhIziswTS
- olatRUxBq6JobY1RY1uaVovVCFi1xWdNCgmxaRSjVNtaH01rU6mIWo3BHypKqdCqEUtQFGWYGeZ9X+ec
- rvXtc+7cAUTb9Ee/fff59t5nv9ba336d6+FzShzHU6gW0B9Nfzl9E/3/gzxA30vfQf+wEii7Pc8Lk/Ah
- 5TMJIPATqa6nv8wSKF3RXnxY3mHhWI61eMOqsgSnDyrDc0viqmdFLDqUpraSQKW8x8YV1rM5Ox31/igl
- r6O/nSS8psih5NN6KOCjqdbQn0M/YntpG/5eeAUbBp9AR9yOD8IdiDz1xCMBPpXzgDSrVVxhOcUtLNE7
- S7VYKgYuFkCjVO27MHUcR+YtHrk4HwRPH3kISEImCtCaacNxNbMwZ8RcnDby7DLbeIaFVrL9l6gPKsN7
- kQgblamvpZ+g+F1d1+D5/JPoo6WxPcS+AwdpAx5YPA37BpZpBlRhpaegna5uWFANnGmBVYxazsAr5kiI
- EjLiSCS4d9J8DZ9hnx3M0M8bsRCXj/4RWmraCqx0MdtPp8cwOYAAVnge1Z/KcQnbSq9jaeel6OT0in1m
- TcFWNMGazxBiEjbgoiAhwsIiIYVNOswihosDLQpSsAbfxQx4jGwcoC6uwzHRDDR7rcxD60OIiGRsCV/D
- jng7uqN9RohPS8lEPi5pWIzvN92MjJd9g5V/lUT0WIOJDOsJGz2dagPB51b33ozHB9ZUgEPg/BQ0ASc6
- gMLSigu080FCgka/QkBiBXJqOm3cwU6eCXAjQZ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BzSASOP/MGG7rnHxt552xX3gOMrwr+virJNZaW4mGcGEHzF
- WqFxrXB+cIkrGLwA4hWpT1qhnX999NCHjbgNGmN7eqzASZabO9dKbNkW6AvIJuR0t29FmwRNYIgHSE6D
- 9vKK7yYU7PTqeg7lr1AFeukaOEgZ6+3CAO1gYNvyfatRRsFwt98lzP4H0inYpEVSpZAAAAAASUVORK5C
- YII='''.strip()
- b64data = [base64.b64decode(b64) for b64 in (b64_on,b64_off)]
- on, off = [PhotoImage(data=img) for img in b64data]
- '''
- # ... Or...
- # Define Our Images
- on = PhotoImage(file="images/on.png")
- off = PhotoImage(file="images/off.png")
- '''
- # Keep track of the button state on/off
- global is_on
- is_on = True
- # Create Label
- my_label = Label(root,
- text="The Switch Is On!",
- fg="green",
- font=("Helvetica", 32))
- my_label.pack(pady=5)
- # Define our switch function
- def switch():
- global is_on
- # Determin is on or off
- if is_on:
- on_button.config(image=off)
- my_label.config(text="The Switch Is Off", fg="white", bg="grey20")
- on_button.config(bg="grey20", activebackground="grey20")
- root.config(bg="grey20")
- is_on = False
- else:
- on_button.config(image=on)
- my_label.config(text="The Switch Is On", fg="green", bg="lightgrey")
- on_button.config(bg="lightgrey", activebackground="lightgrey")
- root.config(bg="lightgrey")
- is_on = True
- # Create A Button
- on_button = Button(root, image=on, bd=0, command=switch)
- on_button.pack(pady=10)
- root.mainloop()
Add Comment
Please, Sign In to add comment