8000 Added a TextBox widget by keflavich · Pull Request #1983 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Added a TextBox widget #1983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
spelling and formatting
  • Loading branch information
Matt Terry committed Jun 5, 2013
commit 5239d7e162da232429cf49d68bea6177599a52dc
33 changes: 18 additions & 15 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1672,8 +1672,8 @@ def __init__(self, ax, s='', enter_callback=None, **text_kwargs):
activate the cursor.

*WARNING* Activating a textbox will remove all other key-press
bindings! They'll be stored in FloatTextBox.old_callbacks and restored
when FloatTextBox.end_text_entry() is called.
bindings! They'll be stored in TextBox.old_callbacks and restored
when TextBox.end_text_entry() is called.

The default widget assumes only numerical data and will not
allow text entry besides numerical characters and ('e','-','.')
Expand All @@ -1684,15 +1684,15 @@ def __init__(self, ax, s='', enter_callback=None, **text_kwargs):
The parent axes for the widget

*s* : str
The initial text of the FloatTextBox. Should be able to be coerced
The initial text of the TextBox. Should be able to be coerced
to a float.

*enter_callback* : function
A function of one argument that will be called with
FloatTextBox.value passed in as the only argument when enter is
A function of one argument that will be called with
TextBox.value passed in as the only argument when enter is
pressed

*text_kwargs* :
*text_kwargs* :
Additional keywork arguments are passed on to self.ax.text()
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I'm not familiar with the numpydoc format but we should conform to that for the sake of consistency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in my branch.

AxesWidget.__init__(self, ax)
Expand All @@ -1701,8 +1701,8 @@ def __init__(self, ax, s='', enter_callback=None, **text_kwargs):
self.ax.set_xticks([])

self.value = float(s)
self.text = self.ax.text(0.025, 0.2, s, transform=self.ax.transAxes,
**text_kwargs)
self.text = self.ax.text(0.025, 0.2, s,
transform=self.ax.transAxes, **text_kwargs)

self.enter_callback = enter_callback
self._cid = None
Expand All @@ -1719,8 +1719,10 @@ def cursor(self):
# solve some of the problems associated
if self._cursor is None:
r = self._get_text_right() # needs a renderer
self._cursor, = self.ax.plot([r,r], [0.2, 0.8],
transform=self.ax.transAxes)
self._cursor = self.ax.plot([r, r],
[0.2, 0.8],
transform=self.ax.transAxes,
)[0]
self._cursor.set_visible(False)
return self._cursor

Expand All @@ -1745,7 +1747,8 @@ def begin_text_entry(self):
for k in keypress_cbs.keys():
self.old_callbacks[k] = keypress_cbs.pop(k)

self._cid = self.canvas.mpl_connect('key_press_event', self.keypress)
self._cid = self.canvas.mpl_connect('key_press_event',
self.keypress)
self.cursor.set_visible(True)
self.redraw()

Expand Down Expand Up @@ -1816,7 +1819,7 @@ def keypress(self, event):
self.set_text(newt, redraw=True)

r = self._get_text_right()
self.cursor.set_xdata([r,r])
self.cursor.set_xdata([r, r])
self.redraw()

def set_text(self, text, redraw=False):
Expand All @@ -1836,11 +1839,11 @@ def set_text(self, text, redraw=False):

def _get_text_right(self):
bbox = self.text.get_window_extent()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This get_window_extent() causes problems on MacOSX. (related to the blitting problems with macosx). You can get around it by calling self.canvas.draw() before get_window_extent(), but that can be laggy. If you check the backend, you can only inflict the lag on macosx users. But then you're putting backend checks into supposedly backend independent widgets.

Any less hacky ideas of how to get the rendered width of a block of text?

l,b,w,h = bbox.bounds
l, b, w, h = bbox.bounds

renderer = self.ax.get_renderer_cache()
en = renderer.points_to_pixels(self.text.get_fontsize()) / 2.

r = l + self._cursorpos*np.ceil(en)
r,t = self.ax.transAxes.inverted().transform((r,b+h))
r = l + self._cursorpos * np.ceil(en)
r, t = self.ax.transAxes.inverted().transform((r, b + h))
return r
0