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