Skip to content

Commit e012f78

Browse files
committed
Added a set_active() for CheckButtons
1 parent a5fccbc commit e012f78

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/matplotlib/widgets.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,24 +569,39 @@ def _clicked(self, event):
569569
if event.inaxes != self.ax:
570570
return
571571

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)):
573573
if (t.get_window_extent().contains(event.x, event.y) or
574574
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)
579576
break
580577
else:
581578
return
582579

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+
583598
if self.drawon:
584599
self.ax.figure.canvas.draw()
585600

586601
if not self.eventson:
587602
return
588603
for cid, func in six.iteritems(self.observers):
589-
func(thist.get_text())
604+
func(self.labels[index].get_text())
590605

591606
def on_clicked(self, func):
592607
"""
@@ -700,19 +715,20 @@ def inside(p):
700715

701716
for i, (p, t) in enumerate(zip(self.circles, self.labels)):
702717
if t.get_window_extent().contains(event.x, event.y) or inside(p):
703-
index_match = i
718+
self.set_active(i)
704719
break
705720
else:
706721
return
707722

708-
self.set_active(index_match)
709-
710723
def set_active(self, index):
711724
"""
712725
Trigger which radio button to make active.
713726
714727
*index* is an index into the original label list
715728
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.
716732
717733
"""
718734
if 0 > index >= len(self.labels):

0 commit comments

Comments
 (0)