Reputation: 952
I'm working on a Python app that uses wxPython for a GUI with an embedded matplotlib figure. It was going really well until I tried to use a matplotlib GUI-neutral widget called a SpanSelector
. Before, I was trying to code this behavior myself, which wasn't going very well, so I was pleased that there was a built-in widget that would take care of it for me. These widgets are supposed to work with any GUI backend, and wxPython is definitely a supported backend.
My code is pretty simple at the moment; I have a defined application class with these (relevant) functions:
def init_gui(self):
...
self.button5 = wx.Button(tab, label="Manual select", size=(120, 30))
self.button5.Bind(wx.EVT_BUTTON, self.get_selection_manual)
...
def get_selection_manual(self, event):
span = matplotlib.widgets.SpanSelector(self.ax, \
self.process_selection_manual, "horizontal", useblit=True, \
rectprops=dict(alpha=0.5, facecolor="red"))
def process_selection_manual(self, vmin, vmax):
print vmin, vmax
If it was working, all it should do is print out the selection the user made after clicking the button. I know that get_selection_manual
is called, but clicking and dragging over the plot never creates a selection, and the callback process_selection_manual
is never called.
I tried putting in a sleep()
and then updating the display. Interestingly, I got it to (sort of) work when my application crashed over a sleep(5)
instead of a time.sleep(5)
. So Python was stalled, but it drew the selection like it was supposed to then. I've never seen that happen before, and I'm not really sure what it means. I haven't found any examples of using wxPython and matplotlib widgets together, so any help is greatly appreciated.
Upvotes: 1
Views: 672
Reputation: 952
This turned out to be a really simple solution: span
has to be declared self.span
... Maybe it was getting garbage-collected before it could do anything? In any case, it works now if I replace
def get_selection_manual(self, event):
span = matplotlib.widgets.SpanSelector(self.ax, \
self.process_selection_manual, "horizontal", useblit=True, \
rectprops=dict(alpha=0.5, facecolor="red"))
with
def get_selection_manual(self, event):
self.span = matplotlib.widgets.SpanSelector(self.ax, \
self.process_selection_manual, "horizontal", \
rectprops=dict(alpha=0.5, facecolor="red"))
I also removed the useblit
flag because it messed up the color; I don't think it was relevant to this fix, but still useful to note.
Upvotes: 3