Skip to content

Commit 3d94620

Browse files
authored
Merge pull request #20544 from Tortar/patch-2
Support of different locations for the text fixing cursor of TextBox
2 parents 3bc2cbd + e7ce925 commit 3d94620

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Text can be positioned inside TextBox widget
2+
--------------------------------------------
3+
4+
A new parameter called *textalignment* can be used to control for the position of the text inside the Axes of the TextBox widget.
5+
6+
.. plot::
7+
8+
from matplotlib import pyplot as plt
9+
from matplotlib.widgets import TextBox
10+
11+
box_input = plt.axes([0.2, 0.2, 0.1, 0.075])
12+
text_box = TextBox(ax=box_input, initial="text", label="", textalignment="center")
13+

examples/widgets/textbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def submit(expression):
4040

4141

4242
axbox = fig.add_axes([0.1, 0.05, 0.8, 0.075])
43-
text_box = TextBox(axbox, "Evaluate")
43+
text_box = TextBox(axbox, "Evaluate", textalignment="center")
4444
text_box.on_submit(submit)
4545
text_box.set_val("t ** 2") # Trigger `submit` with the initial string.
4646

lib/matplotlib/tests/test_widgets.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,14 @@ def test_TextBox():
369369
submit_event = Mock()
370370
text_change_event = Mock()
371371
ax = get_ax()
372-
373-
tool = widgets.TextBox(ax, 'Evaluate')
372+
tool = widgets.TextBox(ax, '')
374373
tool.on_submit(submit_event)
375374
tool.on_text_change(text_change_event)
375+
376+
assert tool.text == ''
377+
378+
do_event(tool, '_click')
379+
376380
tool.set_val('x**2')
377381

378382
assert tool.text == 'x**2'
@@ -382,6 +386,7 @@ def test_TextBox():
382386
tool.stop_typing()
383387

384388
assert submit_event.call_count == 2
389+
385390
do_event(tool, '_click')
386391
do_event(tool, '_keypress', key='+')
387392
do_event(tool, '_keypress', key='5')

lib/matplotlib/widgets.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)