@@ -628,14 +628,14 @@ def disconnect(self, cid):
628
628
except KeyError :
629
629
pass
630
630
631
+
631
632
class TextBox (AxesWidget ):
632
633
"""
633
634
A GUI neutral text input box.
634
635
635
- For the text box to remain responsive
636
- you must keep a reference to it.
636
+ For the text box to remain responsive you must keep a reference to it.
637
637
638
- The following attributes are accessible
638
+ The following attributes are accessible:
639
639
640
640
*ax*
641
641
The :class:`matplotlib.axes.Axes` the button renders into.
@@ -649,11 +649,13 @@ class TextBox(AxesWidget):
649
649
*hovercolor*
650
650
The color of the text box when hovering.
651
651
652
- Call :meth:`on_text_change` to be updated whenever the text changes
653
- Call :meth:`on_submit` to be updated whenever the user hits enter or leaves the text entry field
652
+ Call :meth:`on_text_change` to be updated whenever the text changes.
653
+
654
+ Call :meth:`on_submit` to be updated whenever the user hits enter or
655
+ leaves the text entry field.
654
656
"""
655
657
656
- def __init__ (self , ax , label , initial = '' ,
658
+ def __init__ (self , ax , label , initial = '' ,
657
659
color = '.95' , hovercolor = '1' ):
658
660
"""
659
661
Parameters
@@ -664,48 +666,48 @@ def __init__(self, ax, label, initial = '',
664
666
665
667
label : str
666
668
Label for this text box. Accepts string.
667
-
669
+
668
670
initial : str
669
671
Initial value in the text box
670
-
672
+
671
673
color : color
672
674
The color of the box
673
675
674
676
hovercolor : color
675
677
The color of the box when the mouse is over it
676
678
"""
677
679
AxesWidget .__init__ (self , ax )
678
-
679
- self .DIST_FROM_LEFT = .05
680
-
680
+
681
+ self .DIST_FROM_LEFT = .05
682
+
681
683
self .params_to_disable = []
682
- for key in rcParams .keys ():
684
+ for key in rcParams .keys ():
683
685
if u'keymap' in key :
684
- self .params_to_disable += [key ]
685
-
686
+ self .params_to_disable += [key ]
687
+
686
688
self .text = initial
687
-
688
-
689
-
690
-
691
- self .label = ax .text (0.0 ,0.5 , label ,
689
+ self .label = ax .text (0.0 , 0.5 , label ,
692
690
verticalalignment = 'center' ,
693
691
horizontalalignment = 'right' ,
694
692
transform = ax .transAxes )
695
693
self .text_disp = self ._make_text_disp (self .text )
696
-
694
+
697
695
self .cnt = 0
698
696
self .change_observers = {}
699
697
self .submit_observers = {}
700
-
701
- self .ax .set_xlim (0 , 1 ) #If these lines are removed, the cursor won't appear
702
- self .ax .set_ylim (0 , 1 ) #the first time the box is clicked
703
-
704
- self .cursor_index = 0 ;
705
- self .cursor = self .ax .vlines (0 , 0 , 0 ) #because this is initialized, _render_cursor
706
- self .cursor .set_visible (False ) #can assume that cursor exists
707
-
708
-
698
+
699
+ # If these lines are removed, the cursor won't appear the first
700
+ # time the box is clicked:
701
+ self .ax .set_xlim (0 , 1 )
702
+ self .ax .set_ylim (0 , 1 )
703
+
704
+ self .cursor_index = 0
705
+
706
+ # Because this is initialized, _render_cursor
707
+ # can assume that cursor exists.
708
+ self .cursor = self .ax .vlines (0 , 0 , 0 )
709
+ self .cursor .set_visible (False )
710
+
709
711
self .connect_event ('button_press_event' , self ._click )
710
712
self .connect_event ('button_release_event' , self ._release )
711
713
self .connect_event ('motion_notify_event' , self ._motion )
@@ -718,110 +720,107 @@ def __init__(self, ax, label, initial = '',
718
720
self .hovercolor = hovercolor
719
721
720
722
self ._lastcolor = color
721
-
722
- self .capturekeystrokes = False
723
-
724
-
725
723
726
-
724
+ self .capturekeystrokes = False
725
+
727
726
def _make_text_disp (self , string ):
728
727
return self .ax .text (self .DIST_FROM_LEFT , 0.5 , string ,
729
- verticalalignment = 'center' ,
730
- horizontalalignment = 'left' ,
731
- transform = self .ax .transAxes )
728
+ verticalalignment = 'center' ,
729
+ horizontalalignment = 'left' ,
730
+ transform = self .ax .transAxes )
731
+
732
732
def _rendercursor (self ):
733
- #this is a hack to figure out where the cursor should go.
734
- #we draw the text up to where the cursor should go, measure
735
- #save its dimensions, draw the real text, then put the cursor
736
- #at the saved dimensions
737
-
733
+ # this is a hack to figure out where the cursor should go.
734
+ # we draw the text up to where the cursor should go, measure
735
+ # save its dimensions, draw the real text, then put the cursor
736
+ # at the saved dimensions
737
+
738
738
widthtext = self .text [:self .cursor_index ]
739
739
no_text = False
740
740
if (widthtext == "" or widthtext == " " or widthtext == " " ):
741
741
no_text = widthtext == ""
742
742
widthtext = ","
743
-
744
-
743
+
745
744
wt_disp = self ._make_text_disp (widthtext )
746
-
745
+
747
746
self .ax .figure .canvas .draw ()
748
747
bb = wt_disp .get_window_extent ()
749
748
inv = self .ax .transData .inverted ()
750
749
bb = inv .transform (bb )
751
750
wt_disp .set_visible (False )
752
751
if no_text :
753
- bb [1 , 0 ] = bb [0 , 0 ]
754
- #hack done
752
+ bb [1 , 0 ] = bb [0 , 0 ]
753
+ # hack done
755
754
self .cursor .set_visible (False )
756
-
757
-
755
+
758
756
self .cursor = self .ax .vlines (bb [1 , 0 ], bb [0 , 1 ], bb [1 , 1 ])
759
757
self .ax .figure .canvas .draw ()
760
758
761
759
def _notify_submit_observers (self ):
762
760
for cid , func in six .iteritems (self .submit_observers ):
763
761
func (self .text )
764
-
762
+
765
763
def _release (self , event ):
766
764
if self .ignore (event ):
767
765
return
768
766
if event .canvas .mouse_grabber != self .ax :
769
767
return
770
768
event .canvas .release_mouse (self .ax )
771
-
769
+
772
770
def _keypress (self , event ):
773
771
if self .ignore (event ):
774
772
return
775
773
if self .capturekeystrokes :
776
774
key = event .key
777
-
775
+
778
776
if (len (key ) == 1 ):
779
- self .text = (self .text [:self .cursor_index ] + key +
780
- self .text [self .cursor_index :])
781
- self .cursor_index += 1
777
+ self .text = (self .text [:self .cursor_index ] + key +
778
+ self .text [self .cursor_index :])
779
+ self .cursor_index += 1
782
780
elif key == "right" :
783
- if self .cursor_index != len (self .text ):
784
- self .cursor_index += 1
781
+ if self .cursor_index != len (self .text ):
782
+ self .cursor_index += 1
785
783
elif key == "left" :
786
- if self .cursor_index != 0 :
787
- self .cursor_index -= 1
784
+ if self .cursor_index != 0 :
785
+ self .cursor_index -= 1
788
786
elif key == "home" :
789
- self .cursor_index = 0
787
+ self .cursor_index = 0
790
788
elif key == "end" :
791
- self .cursor_index = len (self .text )
789
+ self .cursor_index = len (self .text )
792
790
elif (key == "backspace" ):
793
791
if self .cursor_index != 0 :
794
- self .text = (self .text [:self .cursor_index - 1 ] +
795
- self .text [self .cursor_index :])
792
+ self .text = (self .text [:self .cursor_index - 1 ] +
793
+ self .text [self .cursor_index :])
796
794
self .cursor_index -= 1
797
795
elif (key == "delete" ):
798
796
if self .cursor_index != len (self .text ):
799
- self .text = (self .text [:self .cursor_index ] +
800
- self .text [self .cursor_index + 1 :])
797
+ self .text = (self .text [:self .cursor_index ] +
798
+ self .text [self .cursor_index + 1 :])
799
+
801
800
self .text_disp .remove ()
802
801
self .text_disp = self ._make_text_disp (self .text )
803
802
self ._rendercursor ()
804
803
for cid , func in six .iteritems (self .change_observers ):
805
804
func (self .text )
806
805
if key == "enter" :
807
- self ._notify_submit_observers ()
808
-
806
+ self ._notify_submit_observers ()
807
+
809
808
def _click (self , event ):
810
809
if self .ignore (event ):
811
810
return
812
811
if event .inaxes != self .ax :
813
- notifysubmit = False
814
- #because _notify_submit_users might throw an error in the
815
- #user's code, we only want to call it once we've already done
816
- #our cleanup.
812
+ notifysubmit = False
813
+ # because _notify_submit_users might throw an error in the
814
+ # user's code, we only want to call it once we've already done
815
+ # our cleanup.
817
816
if self .capturekeystrokes :
818
- for key in self .params_to_disable :
817
+ for key in self .params_to_disable :
819
818
rcParams [key ] = self .reset_params [key ]
820
- notifysubmit = True
819
+ notifysubmit = True
821
820
self .capturekeystrokes = False
822
821
self .cursor .set_visible (False )
823
822
self .ax .figure .canvas .draw ()
824
-
823
+
825
824
if notifysubmit :
826
825
self ._notify_submit_observers ()
827
826
return
@@ -838,7 +837,6 @@ def _click(self, event):
838
837
self .cursor_index = len (self .text )
839
838
self ._rendercursor ()
840
839
841
-
842
840
def _motion (self , event ):
843
841
if self .ignore (event ):
844
842
return
@@ -854,31 +852,35 @@ def _motion(self, event):
854
852
855
853
def on_text_change (self , func ):
856
854
"""
857
- When the text changes, call this *func* with event
855
+ When the text changes, call this *func* with event.
858
856
859
- A connection id is returned which can be used to disconnect
857
+ A connection id is returned which can be used to disconnect.
860
858
"""
861
859
cid = self .cnt
862
860
self .change_observers [cid ] = func
863
861
self .cnt += 1
864
862
return cid
863
+
865
864
def on_submit (self , func ):
866
865
"""
867
- When the user hits enter or leaves the submision box, call this *func* with event
866
+ When the user hits enter or leaves the submision box, call this
867
+ *func* with event.
868
868
869
- A connection id is returned which can be used to disconnect
869
+ A connection id is returned which can be used to disconnect.
870
870
"""
871
871
cid = self .cnt
872
872
self .submit_observers [cid ] = func
873
873
self .cnt += 1
874
874
return cid
875
+
875
876
def disconnect (self , cid ):
876
877
"""remove the observer with connection id *cid*"""
877
878
try :
878
879
del self .observers [cid ]
879
880
except KeyError :
880
881
pass
881
882
883
+
882
884
class RadioButtons (AxesWidget ):
883
885
"""
884
886
A GUI neutral radio button.
@@ -1176,7 +1178,6 @@ def funchspace(self, val):
1176
1178
self .targetfig .canvas .draw ()
1177
1179
1178
1180
1179
-
1180
1181
class Cursor (AxesWidget ):
1181
1182
"""
1182
1183
A horizontal and vertical line that spans the axes and moves with
0 commit comments