@@ -569,24 +569,39 @@ def _clicked(self, event):
569
569
if event .inaxes != self .ax :
570
570
return
571
571
572
- for p , t , lines in zip (self .rectangles , self .labels , self . lines ):
572
+ for i , ( p , t ) in enumerate ( zip (self .rectangles , self .labels ) ):
573
573
if (t .get_window_extent ().contains (event .x , event .y ) or
574
574
p .get_window_extent ().contains (event .x , event .y )):
575
- l1 , l2 = lines
576
- l1 .set_visible (not l1 .get_visible ())
577
- l2 .set_visible (not l2 .get_visible ())
578
- thist = t
575
+ self .set_active (i )
579
576
break
580
577
else :
581
578
return
582
579
580
+ def set_active (self , index ):
581
+ """
582
+ Directly (de)activate a check button by index.
583
+
584
+ *index* is an index into the original label list
585
+ that this object was constructed with.
586
+ Raises ValueError if *index* is invalid.
587
+
588
+ Callbacks will be triggered if :attr:`eventson` is True.
589
+
590
+ """
591
+ if 0 > index >= len (self .labels ):
592
+ raise ValueError ("Invalid CheckButton index: %d" % index )
593
+
594
+ l1 , l2 = self .lines [index ]
595
+ l1 .set_visible (not l1 .get_visible ())
596
+ l2 .set_visible (not l2 .get_visible ())
597
+
583
598
if self .drawon :
584
599
self .ax .figure .canvas .draw ()
585
600
586
601
if not self .eventson :
587
602
return
588
603
for cid , func in six .iteritems (self .observers ):
589
- func (thist .get_text ())
604
+ func (self . labels [ index ] .get_text ())
590
605
591
606
def on_clicked (self , func ):
592
607
"""
@@ -700,19 +715,20 @@ def inside(p):
700
715
701
716
for i , (p , t ) in enumerate (zip (self .circles , self .labels )):
702
717
if t .get_window_extent ().contains (event .x , event .y ) or inside (p ):
703
- index_match = i
718
+ self . set_active ( i )
704
719
break
705
720
else :
706
721
return
707
722
708
- self .set_active (index_match )
709
-
710
723
def set_active (self , index ):
711
724
"""
712
725
Trigger which radio button to make active.
713
726
714
727
*index* is an index into the original label list
715
728
that this object was constructed with.
729
+ Raise ValueError if the index is invalid.
730
+
731
+ Callbacks will be triggered if :attr:`eventson` is True.
716
732
717
733
"""
718
734
if 0 > index >= len (self .labels ):
0 commit comments