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
8000
Prev Previous commit
Next Next commit
use on_changed() convention. use drawon, too
  • Loading branch information
Matt Terry committed Jun 10, 2013
commit a1e0207e478589208bad6a0e622e7f0262c8a0d9
48 changes: 36 additions & 12 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,8 @@ def __init__(self, ax, s='', allowed_chars=None, type=str,

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

Call :meth:`on_onchanged` to connect to TextBox updates
"""
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)
self.ax.set_navigate(False)
Expand All @@ -1720,9 +1722,28 @@ def __init__(self, ax, s='', allowed_chars=None, type=str,
self._cursorpos = len(self.text.get_text())
self.old_callbacks = {}

self.cnt = 0
self.observers = {}

self.connect_event('button_press_event', self._mouse_activate)
self.redraw_callbacks = []
self.redraw()

def on_changed(self, func):
"""
When the textbox changes self.value, call *func* with the new value.

A connection id is returned with can be used to disconnect.
"""
cid = self.cnt
self.observers[cid] = func
self.cnt += 1
return cid

def disconnect(self, cid):
"""remove the observer with connection id *cid*"""
try:
del self.observers[cid]
except KeyError:
pass

@property
def cursor(self):
Expand All @@ -1734,13 +1755,8 @@ def cursor(self):
self._cursor.set_visible(False)
return self._cursor

def redraw(self):
for f in self.redraw_callbacks:
f()
self.canvas.draw()

def _mouse_activate(self, event):
if self.ignore(event):
if self.ignore(event) or not self.eventson:
return
if self.ax == event.inaxes:
self.begin_text_entry()
Expand All @@ -1757,7 +1773,8 @@ def begin_text_entry(self):
self._cid = self.canvas.mpl_connect('key_press_event',
self.keypress)
self.cursor.set_visible(True)
self.redraw()
if self.drawon:
self.canvas.draw()

def end_text_entry(self):
keypress_cbs = self.canvas.callbacks.callbacks['key_press_event']
Expand All @@ -1767,13 +1784,14 @@ def end_text_entry(self):
keypress_cbs[k] = self.old_callbacks.pop(k)

self.cursor.set_visible(False)
self.redraw()
if self.drawon:
self.canvas.draw()

def keypress(self, event):
"""
Parse a keypress - only allow #'s!
"""
if self.ignore(event):
if self.ignore(event) or not self.eventson:
return

newt = t = self.text.get_text()
Expand Down Expand Up @@ -1812,16 +1830,22 @@ def keypress(self, event):
self.set_text(newt)
x, y = self._get_cursor_endpoints()
self.cursor.set_xdata(x)
self.redraw()
if self.drawon:
self.canvas.draw()

def set_text(self, text):
success = False
try:
# only try to update if there's a real value
self.value = self.type(text)
success = True
except ValueError:
pass
# but always change the text
self.text.set_text(text)
if success and self.eventson:
for func in self.observers.itervalues():
func(self.value)

def _get_cursor_endpoints(self):
# to get cursor position
Expand Down
0