@@ -1119,9 +1119,11 @@ class TextBox(AxesWidget):
1119
1119
lambda self : self ._observers .callbacks ['change' ]))
1120
1120
submit_observers = _api .deprecated ("3.4" )(property (
1121
1121
lambda self : self ._observers .callbacks ['submit' ]))
1122
+ DIST_FROM_LEFT = _api .deprecate_privatize_attribute ("3.5" )
1122
1123
1123
1124
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" ):
1125
1127
"""
1126
1128
Parameters
1127
1129
----------
@@ -1137,20 +1139,26 @@ def __init__(self, ax, label, initial='',
1137
1139
The color of the box when the mouse is over it.
1138
1140
label_pad : float
1139
1141
The distance between the label and the right side of the textbox.
1142
+ textalignment : {'left', 'center', 'right'}
1143
+ The horizontal location of the text.
1140
1144
"""
1141
1145
super ().__init__ (ax )
1142
1146
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 )
1144
1152
1145
1153
self .label = ax .text (
1146
1154
- label_pad , 0.5 , label , transform = ax .transAxes ,
1147
1155
verticalalignment = 'center' , horizontalalignment = 'right' )
1148
1156
1149
1157
# TextBox's text object should not parse mathtext at all.
1150
1158
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 )
1154
1162
1155
1163
self ._observers = cbook .CallbackRegistry ()
1156
1164
@@ -1193,12 +1201,22 @@ def _rendercursor(self):
1193
1201
1194
1202
text = self .text_disp .get_text () # Save value before overwriting it.
1195
1203
widthtext = text [:self .cursor_index ]
1204
+
1205
+ bb_text = self .text_disp .get_window_extent ()
1196
1206
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
+
1200
1217
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 )
1202
1220
self .text_disp .set_text (text )
1203
1221
1204
1222
self .ax .figure .canvas .draw ()
0 commit comments