I think the slider should update with the display if
> the dragging is enabled, so the slider value stays in
> sync. The typical user will understand that things will
> be a little sliggish when plotting insane data such as
> you example. I am wanting to create a simple text/value
> entry field, but I noticed that the backends don't
> support the delete key in events. Are there issues
> across platforms with this? If not, I will probably add
> this.
Yes, you should add the delete key. I was hacking a little bit
yesterday on text entry. An alternative to creating a text widget is
to simply require each backend to define a few gui dependent functions
that we don't to get involved with (get_filename, get_text, etc..) and
then connect these to a button. The button could say something like
···
--------------------
xlabel: volts(s) |
--------------------
and when you click on it calls get_text from the backend and updates
"volts(s)" with the text you supply (and in this example would update
the xlabel too).
Below is a buggy and incomplete example of using the region/cache/copy
stuff I mentioned before to type into an axes. As before, it is
gtkagg only at this point. In particular, the cursor is pretty
crappy. Note I am using "None" which is what backspace currently
defines, to indicate the backspace key. One thing that would make
this work better is to predefine the region the text goes into (an
Axes presumably) and just blit the entire axes bbox before
re-rendering the string with each character. This would solve the
cursor erase on backspace bug you will notice if you try it.
This was just screwing around and not meant for public consumption,
but since you brought it up
Anyone want to write a word processor in matplotlib <wink>?
## Example text entry ##
from pylab import *
from matplotlib.transforms import lbwh_to_bbox, identity_transform
class TextBox:
def __init__(self, canvas, s='Type: '):
self.canvas = canvas
self.text = text(0.5, 0.5, s)
draw()
canvas.mpl_connect('key_press_event', self.keypress)
self.region = canvas.copy_from_bbox(self.get_padded_box())
l,b,r,t = self.get_cursor_position()
self.cursor, = plot([r,r], [b,t])
def keypress(self, event):
if event.key is not None and len(event.key)>1: return
t = self.text.get_text()
if event.key is None: # simulate backspace
if len(t): newt = t[:-1]
else: newt = ''
else:
newt = t + event.key
oldbox = self.get_padded_box()
self.text.set_text(newt)
newbox = self.get_padded_box()
l,b,r,t = self.get_cursor_position()
self.cursor.set_xdata([r,r])
self.cursor.set_ydata([b,t])
canvas = self.canvas
canvas.restore_region(self.region)
self.region = canvas.copy_from_bbox(newbox)
f.draw_artist(self.text)
f.draw_artist(self.cursor)
canvas.blit(oldbox)
canvas.blit(newbox)
def get_padded_box(self, pad=5):
l,b,w,h = self.text.get_window_extent().get_bounds()
return lbwh_to_bbox(l-pad, b-pad, w+2*pad, h+2*pad)
def get_cursor_position(self):
l,b,w,h = self.text.get_window_extent().get_bounds()
r = l+w+5
t = b+h
l,b = self.text.get_transform().inverse_xy_tup((l,b))
r,t = self.text.get_transform().inverse_xy_tup((r,t))
return l,b,r,t
ion()
f = figure()
title('Start typing')
axis([0, 2, 0, 1])
box = TextBox(f.canvas)
axis([0, 2, 0, 1])
show()