@@ -1119,9 +1119,11 @@ class TextBox(AxesWidget):
11191119 lambda self : self ._observers .callbacks ['change' ]))
11201120 submit_observers = _api .deprecated ("3.4" )(property (
11211121 lambda self : self ._observers .callbacks ['submit' ]))
1122+ DIST_FROM_LEFT = _api .deprecate_privatize_attribute ("3.5" )
11221123
11231124 def __init__ (self , ax , label , initial = '' ,
1124- color = '.95' , hovercolor = '1' , label_pad = .01 ):
1125+ color = '.95' , hovercolor = '1' , label_pad = .01 ,
1126+ textalignment = "left" ):
11251127 """
11261128 Parameters
11271129 ----------
@@ -1137,20 +1139,26 @@ def __init__(self, ax, label, initial='',
11371139 The color of the box when the mouse is over it.
11381140 label_pad : float
11391141 The distance between the label and the right side of the textbox.
1142+ textalignment : {'left', 'center', 'right'}
1143+ The horizontal location of the text.
11401144 """
11411145 super ().__init__ (ax )
11421146
1143- self .DIST_FROM_LEFT = .05
1147+ self ._DIST_FROM_LEFT = .05
1148+
1149+ self ._text_position = _api .check_getitem (
1150+ {"left" : 0.05 , "center" : 0.5 , "right" : 0.95 },
1151+ textalignment = textalignment )
11441152
11451153 self .label = ax .text (
11461154 - label_pad , 0.5 , label , transform = ax .transAxes ,
11471155 verticalalignment = 'center' , horizontalalignment = 'right' )
11481156
11491157 # TextBox's text object should not parse mathtext at all.
11501158 self .text_disp = self .ax .text (
1151- self .DIST_FROM_LEFT , 0.5 , initial ,
1152- transform = self . ax . transAxes , verticalalignment = 'center' ,
1153- horizontalalignment = 'left' , parse_math = False )
1159+ self ._text_position , 0.5 , initial , transform = self . ax . transAxes ,
1160+ verticalalignment = 'center' , horizontalalignment = textalignment ,
1161+ parse_math = False )
11541162
11551163 self ._observers = cbook .CallbackRegistry ()
11561164
@@ -1193,12 +1201,22 @@ def _rendercursor(self):
11931201
11941202 text = self .text_disp .get_text () # Save value before overwriting it.
11951203 widthtext = text [:self .cursor_index ]
1204+
1205+ bb_text = self .text_disp .get_window_extent ()
11961206 self .text_disp .set_text (widthtext or "," )
1197- bb = self .text_disp .get_window_extent ()
1198- if not widthtext : # Use the comma for the height, but keep width to 0.
1199- bb .x1 = bb .x0
1207+ bb_widthtext = self .text_disp .get_window_extent ()
1208+
1209+ if bb_text .y0 == bb_text .y1 : # Restoring the height if no text.
1210+ bb_text .y0 -= (bb_widthtext .y1 - bb_widthtext .y0 )/ 2
1211+ bb_text .y1 += (bb_widthtext .y1 - bb_widthtext .y0 )/ 2
1212+ elif not widthtext : # Keep width to 0.
1213+ bb_text .x1 = bb_text .x0
1214+ else : # Move the cursor using width of bb_widthtext.
1215+ bb_text .x1 = bb_text .x0 + (bb_widthtext .x1 - bb_widthtext .x0 )
1216+
12001217 self .cursor .set (
1201- segments = [[(bb .x1 , bb .y0 ), (bb .x1 , bb .y1 )]], visible = True )
1218+ segments = [[(bb_text .x1 , bb_text .y0 ), (bb_text .x1 , bb_text .y1 )]],
1219+ visible = True )
12021220 self .text_disp .set_text (text )
12031221
12041222 self .ax .figure .canvas .draw ()
0 commit comments