@@ -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 :
0 commit comments