May-11-2021, 06:52 PM
Just wanted to share a little program using tkinter and images in hoping maybe it will help someone.
The images:
![[Image: on.png]](https://my-python.org/images/python-forum/on.png)
![[Image: off.png]](https://my-python.org/images/python-forum/off.png)
![[Image: switch_off.png]](https://my-python.org/images/python-forum/switch_off.png)
![[Image: switch_on.png]](https://my-python.org/images/python-forum/switch_on.png)
The code:
The images:
![[Image: on.png]](https://my-python.org/images/python-forum/on.png)
![[Image: off.png]](https://my-python.org/images/python-forum/off.png)
![[Image: switch_off.png]](https://my-python.org/images/python-forum/switch_off.png)
![[Image: switch_on.png]](https://my-python.org/images/python-forum/switch_on.png)
The code:
#! /usr/bin/env python3
import tkinter as tk
from functools import partial
class MyClass:
def __init__(self, parent):
self.parent = parent
self.parent.columnconfigure(0, weight=1)
self.parent.rowconfigure(0, weight=1)
self.frame = tk.Frame(self.parent)
self.frame.grid(column=0, row=0, sticky='news')
self.frame.grid_columnconfigure(0, weight=3)
self.frame.grid_rowconfigure(0, weight=3)
bulb = tk.PhotoImage(file='off.png')
bulb.img = bulb
switch = tk.PhotoImage(file='switch_off.png')
switch.img = switch
self.light = tk.Label(self.frame, image=bulb)
self.light.grid(column=0, row=0, sticky='news')
self.switch = tk.Label(self.frame, image=switch)
self.switch.grid(column=0, row=1, sticky='news')
is_clicked = True
self.switch.bind('<ButtonRelease-1>', partial(self.lightswitch, is_clicked))
# self.switch.bind('<ButtonRelease-1>', partial(self.lightswitch, 'off'))
def lightswitch(self, value, event):
is_clicked = True
if value:
bulb = tk.PhotoImage(file='on.png')
bulb.img = bulb
self.light['image'] = bulb
switch = tk.PhotoImage(file='switch_on.png')
switch.img = switch
self.switch['image'] = switch
self.is_clicked = False
self.switch.bind('<ButtonRelease-1>', partial(self.lightswitch, self.is_clicked))
else:
bulb = tk.PhotoImage(file='off.png')
bulb.img = bulb
self.light['image'] = bulb
switch = tk.PhotoImage(file='switch_off.png')
switch.img = switch
self.switch['image'] = switch
self.is_clicked = True
self.switch.bind('<ButtonRelease-1>', partial(self.lightswitch, self.is_clicked))
def main():
root = tk.Tk()
root.title('Light On/Off')
root.geometry('+250+200')
MyClass(root)
root.mainloop()
if __name__ == '__main__':
main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
