Skip to content

Commit e004219

Browse files
Improved LED functionality, see slightlynybbled#30. Making some variables private
1 parent a7874de commit e004219

4 files changed

Lines changed: 66 additions & 23 deletions

File tree

examples/leds.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,44 @@
22
import tk_tools
33

44

5+
def on_click_callback(on):
6+
if on:
7+
print('led is on')
8+
else:
9+
print('led is off')
10+
11+
512
if __name__ == '__main__':
613

714
root = tk.Tk()
815

9-
led = tk_tools.Led(root, size=50)
10-
led.pack()
16+
led0 = tk_tools.Led(root, size=50, on_click_callback=on_click_callback)
17+
led0.pack()
1118

12-
tk.Button(root, text='red', command=led.to_red).pack(fill=tk.X)
19+
tk.Button(root, text='red', command=led0.to_red).pack(fill=tk.X)
1320

1421
tk.Button(root,
1522
text='red on',
16-
command=lambda: led.to_red(True)).pack(fill=tk.X)
23+
command=lambda: led0.to_red(True)).pack(fill=tk.X)
1724

18-
tk.Button(root, text='green', command=led.to_green).pack(fill=tk.X)
25+
tk.Button(root, text='green', command=led0.to_green).pack(fill=tk.X)
1926

2027
tk.Button(root,
2128
text='green on',
22-
command=lambda: led.to_green(True)).pack(fill=tk.X)
29+
command=lambda: led0.to_green(True)).pack(fill=tk.X)
2330

24-
tk.Button(root, text='yellow', command=led.to_yellow).pack(fill=tk.X)
31+
tk.Button(root, text='yellow', command=led0.to_yellow).pack(fill=tk.X)
2532

2633
tk.Button(root,
2734
text='yellow on',
28-
command=lambda: led.to_yellow(True)).pack(fill=tk.X)
35+
command=lambda: led0.to_yellow(True)).pack(fill=tk.X)
36+
37+
tk.Button(root, text='grey', command=led0.to_grey).pack(fill=tk.X)
38+
39+
tk.Label(root, text='Clickable LED').pack(fill=tk.X)
2940

30-
tk.Button(root, text='grey', command=led.to_grey).pack(fill=tk.X)
41+
led1 = tk_tools.Led(root, size=50, on_click_callback=on_click_callback, toggle_on_click=True)
42+
led1.to_green()
43+
led1.pack()
3144

3245
root.mainloop()

tk_tools/canvas.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def frange(start, stop, step, digits_to_round=3):
459459
start += step
460460

461461

462-
class Led(tk.Frame):
462+
class Led(ttk.Frame):
463463
"""
464464
Create an LED-like interface for the user.::
465465
@@ -469,41 +469,62 @@ class Led(tk.Frame):
469469
led.to_red()
470470
led.to_green(on=True)
471471
472+
The user also has the option of adding an `on_click_callback`
473+
function. When the button is clicked, the button will change
474+
state and the on-click callback will be executed. The
475+
callback must accept a single boolean parameter, `on`, which
476+
indicates if the LED was just turned on or off.
477+
472478
:param parent: the parent frame
473479
:param size: the size in pixels
480+
:param on_click_callback: a callback which accepts a boolean parameter 'on'
474481
:param options: the frame options
475482
"""
476-
def __init__(self, parent, size=100, **options):
483+
def __init__(self, parent, size=100, on_click_callback=None, toggle_on_click=False, **options):
477484
self._parent = parent
478-
super().__init__(self._parent, padx=3, pady=3, borderwidth=2,
485+
super().__init__(self._parent, padding=3, borderwidth=2,
479486
**options)
480487

481-
self.size = size
488+
self._size = size
482489

483-
self.canvas = tk.Canvas(self, width=self.size, height=self.size)
484-
self.canvas.grid(row=0)
485-
self.image = None
490+
self._canvas = tk.Canvas(self, width=self._size, height=self._size)
491+
self._canvas.grid(row=0)
492+
self._image = None
493+
self._on = False
494+
self._user_click_callback = on_click_callback
495+
self._toggle_on_click = toggle_on_click
486496

487497
self.to_grey()
488498

499+
def on_click(e):
500+
if self._user_click_callback is not None:
501+
self._user_click_callback(self._on)
502+
503+
self._canvas.bind('<Button-1>', on_click)
504+
489505
def _load_new(self, img_data: str):
490506
"""
491507
Load a new image.
492508
493509
:param img_data: the image data as a base64 string
494510
:return: None
495511
"""
496-
self.image = tk.PhotoImage(data=img_data)
497-
self.image = self.image.subsample(int(200 / self.size),
498-
int(200 / self.size))
499-
self.canvas.create_image(0, 0, image=self.image, anchor='nw')
512+
self._image = tk.PhotoImage(data=img_data)
513+
self._image = self._image.subsample(int(200 / self._size),
514+
int(200 / self._size))
515+
self._canvas.create_image(0, 0, image=self._image, anchor='nw')
500516

501-
def to_grey(self):
517+
if self._user_click_callback is not None:
518+
self._user_click_callback(self._on)
519+
520+
def to_grey(self, on: bool=False):
502521
"""
503522
Change the LED to grey.
504523
524+
:param on: Unused, here for API consistency with the other states
505525
:return: None
506526
"""
527+
self._on = False
507528
self._load_new(led_grey)
508529

509530
def to_green(self, on: bool=False):
@@ -513,17 +534,25 @@ def to_green(self, on: bool=False):
513534
:param on: True or False
514535
:return: None
515536
"""
537+
self._on = on
516538
if on:
517539
self._load_new(led_green_on)
540+
541+
if self._toggle_on_click:
542+
self._canvas.bind('<Button-1>', lambda x: self.to_green(False))
518543
else:
519544
self._load_new(led_green)
520545

546+
if self._toggle_on_click:
547+
self._canvas.bind('<Button-1>', lambda x: self.to_green(True))
548+
521549
def to_red(self, on: bool=False):
522550
"""
523551
Change the LED to red (on or off)
524552
:param on: True or False
525553
:return: None
526554
"""
555+
self._on = on
527556
if on:
528557
self._load_new(led_red_on)
529558
else:
@@ -535,6 +564,7 @@ def to_yellow(self, on: bool=False):
535564
:param on: True or False
536565
:return: None
537566
"""
567+
self._on = on
538568
if on:
539569
self._load_new(led_yellow_on)
540570
else:

tk_tools/groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def __init__(self, parent, callback=None, **kwargs):
615615
self.__setup_styles() # creates custom styles
616616
self.__place_widgets() # pack/grid used widgets
617617
self.__config_calendar() # adjust calendar columns and setup tags
618-
# configure a canvas, and proper bindings, for selecting dates
618+
# configure a _canvas, and proper bindings, for selecting dates
619619
self.__setup_selection(sel_bg, sel_fg)
620620

621621
# store items ids, used for insertion later

tk_tools/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.8.2'
1+
__version__ = '0.9.0'

0 commit comments

Comments
 (0)