This turned up when I generated blowups of portions of a thumbnail image by
clicking on various locations. The blowups contained a button, but the
button was inoperative. My guess is that when the click handler that created
the blowup exited it took the button object with it. My workaround was to
create the various button handlers in the Button __init__ method so that
they hung onto a reference to self. This may not be the best approach so
I've attached a file for discussion.
Chuck
"""
GUI Neutral widgets
All of these widgets require you to predefine an Axes instance and
pass that as the first arg. matplotlib doesn't try to be too smart in
layout -- you have to figure out how wide and tall you want your Axes
to be to accommodate your widget.
"""
import numpy as np
class Widget:
"""
OK, I couldn't resist; abstract base class for mpl GUI neutral
widgets
"""
drawon = True
eventson = True
class Button(Widget):
"""
A GUI neutral button
The following attributes are accesible
ax - the Axes the button renders into
label - a text.Text instance
color - the color of the button when not hovering
hovercolor - the color of the button when hovering
Call "on_clicked" to connect to the button
"""
def __init__(self, ax, label, image=None,
color='0.85', hovercolor='0.95'):
"""
ax is the Axes instance the button will be placed into
label is a string which is the button text
image if not None, is an image to place in the button -- can
be any legal arg to imshow (numpy array, matplotlib Image
instance, or PIL image)
color is the color of the button when not activated
hovercolor is the color of the button when the mouse is over
it
"""
def click(event):
if event.inaxes != self.ax:
return
if not self.eventson:
return
for cid, func in self.observers.items():
func(event)
def motion(event):
if event.inaxes==self.ax:
c = self.hovercolor
else:
c = self.color
if c != self.lastcolor:
self.ax.set_axis_bgcolor(c)
self.lastcolor = c
if self.drawon: self.ax.figure.canvas.draw()
if image is not None:
ax.imshow(image)
self.label = ax.text(0.5, 0.5, label,
verticalalignment='center',
horizontalalignment='center',
transform=ax.transAxes)
self.cnt = 0
self.ax = ax
self.color = color
self.hovercolor = hovercolor
self.lastcolor = color
self.observers = {}
ax.figure.canvas.mpl_connect('button_press_event', click)
ax.figure.canvas.mpl_connect('motion_notify_event', motion)
ax.set_navigate(False)
ax.set_axis_bgcolor(color)
ax.set_xticks([])
ax.set_yticks([])
def on_clicked(self, func):
"""
When the button is clicked, call this func with event
A connection id is returned which 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
------------------------------------------------------------------------------
5 Ways to Improve & Secure Unified Communications
Unified Communications promises greater efficiencies for business. UC can
improve internal communications as well as offer faster, more efficient ways
to interact with customers and streamline customer service. Learn more!
http://www.accelacomm.com/jaw/sfnl/114/51426253/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel