On 6/5/07, Mark Bakker <[EMAIL PROTECTED]> wrote:

I think a prompt could be very useful in MPL, just to build small little
GUI's that only need 1 or 2 boxes.
I also realize it is not easy, and for bigger jobs you want a full GUI
environment like wx or Tk anyway, so I understand it when developers set
other priorities.
Then again, I would really use it,
Mark

> From: Matthias Michler < [EMAIL PROTECTED]>
>
> Hello everybody,
>
> Now my question is: Could a prompt be a useful part of matplotlib?

I worked on this some time ago, I never got to the point where I
thought it was ready for production but it is  close.  There is a
problem if usetex is enabled, because partial tex strings will cause
errors.  But you can use it with regular text or plain text.

Give it  a whirl -- I don't mind including tit in matplotlib.widgets
if people think it would be usefult.  I do think there is a niche for
these kinds of things where people want brain dead simple widgets w/o
having to deal with GUI toolkits.

I'm pasting the example below, and also attaching it in case the lines
get wrapped

# usetex not supported
import matplotlib
matplotlib.rcParams['text.usetex'] = False

import matplotlib.transforms as transforms
import matplotlib.widgets as widgets            
from pylab import figure, show, nx


class TextBox(widgets.Widget):
   def __init__(self, ax, s=''):
       self.canvas = ax.figure.canvas
       self.text = ax.text(0.025, 0.2, s,
                           fontsize=14,
                           #verticalalignment='baseline',
                           horizontalalignment='left',
                           transform=ax.transAxes)
       self.ax = ax
       ax.set_yticks([])
       ax.set_xticks([])

       ax.set_navigate(False)
       self.canvas.draw()
       self.canvas.mpl_connect('key_press_event', self.keypress)

       self.region = self.canvas.copy_from_bbox(ax.bbox)

       r = self._get_text_right()
       self.cursor, = ax.plot([r,r], [0.2, 0.8], transform=ax.transAxes)
       self.redraw()

   def redraw(self):
       self.ax.redraw_in_frame()
       self.canvas.blit(self.ax.bbox)


   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

       self.text.set_text(newt)

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


   def _get_text_right(self):
       l,b,w,h = self.text.get_window_extent().get_bounds()
       r = l+w+2
       t = b+h
       s = self.text.get_text()
       # adjust cursor position for trailing space
       numtrail = len(s) - len(s.rstrip())
       en = 
self.ax.get_renderer_cache().points_to_pixels(self.text.get_fontsize())/2.

       r += numtrail*en
       l,b = self.ax.transAxes.inverse_xy_tup((l,b))
       r,t = self.ax.transAxes.inverse_xy_tup((r,t))
       return r




fig = figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.7])
ax.plot([1,2,3])

#rc('text', usetex=1)
fig.text(0.39, 0.875, 'My label: ',
      horizontalalignment='right', verticalalignment='center')
axtext = fig.add_axes([0.4, 0.85, 0.5, 0.05])

box = TextBox(axtext)

show()
# usetex not supported
import matplotlib
matplotlib.rcParams['text.usetex'] = False

import matplotlib.transforms as transforms
import matplotlib.widgets as widgets		
from pylab import figure, show, nx


class TextBox(widgets.Widget):
    def __init__(self, ax, s=''):
        self.canvas = ax.figure.canvas
        self.text = ax.text(0.025, 0.2, s,
                            fontsize=14,
                            #verticalalignment='baseline',
                            horizontalalignment='left',
                            transform=ax.transAxes)
        self.ax = ax
        ax.set_yticks([])
        ax.set_xticks([])
        
        ax.set_navigate(False)
        self.canvas.draw()
        self.canvas.mpl_connect('key_press_event', self.keypress)        

        self.region = self.canvas.copy_from_bbox(ax.bbox)
        
        r = self._get_text_right()
        self.cursor, = ax.plot([r,r], [0.2, 0.8], transform=ax.transAxes)
        self.redraw()

    def redraw(self):
        self.ax.redraw_in_frame()
        self.canvas.blit(self.ax.bbox)

        
    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
            
        self.text.set_text(newt)

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

        
    def _get_text_right(self):
        l,b,w,h = self.text.get_window_extent().get_bounds()
        r = l+w+2
        t = b+h
        s = self.text.get_text()
        # adjust cursor position for trailing space
        numtrail = len(s) - len(s.rstrip())
        en = self.ax.get_renderer_cache().points_to_pixels(self.text.get_fontsize())/2.

        r += numtrail*en
        l,b = self.ax.transAxes.inverse_xy_tup((l,b))
        r,t = self.ax.transAxes.inverse_xy_tup((r,t))
        return r
        
        
        

fig = figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.7])
ax.plot([1,2,3])

#rc('text', usetex=1)
fig.text(0.39, 0.875, 'My label: ',
       horizontalalignment='right', verticalalignment='center')
axtext = fig.add_axes([0.4, 0.85, 0.5, 0.05])

box = TextBox(axtext)

show()
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to