Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This python program plots a figure in a wxpython window. It runs fine in Enthought Python Distribution 7.3-2, but when I close the figure, python keeps running and doesn't exit.

What am I missing? When I create wxpython GUIs that don't use matplotlib, they exit correctly after I close the window. I guess there must be something about message loops in wxpython that I don't understand.

# adapted from:
# http://wiki.wxpython.org/Getting%20Started
# http://www.cs.colorado.edu/~kena/classes/5448/s11/presentations/pearse.pdf

import wx
import pylab as pl
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class GUIPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.parent = parent

        # create some sizers
        sizer = wx.BoxSizer(wx.VERTICAL)

        # A button
        self.button =wx.Button(self, label="Tada!")
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)

        # put up a figure
        self.figure = pl.figure()
        self.axes = self.drawplot(self.figure)
        self.canvas = FigureCanvas(self, -1, self.figure)

        sizer.Add(self.canvas, 0, wx.ALIGN_CENTER|wx.ALL)
        sizer.Add(self.button, 0, wx.ALIGN_CENTER|wx.ALL)

        self.SetSizerAndFit(sizer)  
    def log(self, fmt, *args):
        print (fmt % args)
    def OnClick(self,event):
        self.log("button clicked, id#%d\n", event.GetId())
    def drawplot(self, fig):
        ax = fig.add_subplot(1,1,1)
        t = pl.arange(0,1,0.001)
        ax.plot(t,t*t)
        ax.grid()
        return ax

app = wx.App(False)
frame = wx.Frame(None)
panel = GUIPanel(frame)
frame.Fit()
frame.Center()
frame.Show()
app.MainLoop()
share|improve this question
See matplotlib.org/examples/user_interfaces. It looks like you are creating figures using pylab, which has it's own event loop which is what is messing you up. – tcaswell Apr 10 at 16:27
pylab has an event loop that's different from matplotlib? ick. – Jason S Apr 10 at 19:56
no it is an event loop that is separate from the one you start in app.MainLoop. pylab is matplotlib, just with a bit of magic on top. – tcaswell Apr 10 at 20:00
Where is the functionality of the "magic" documented? I'm uncomfortable with the idea that pylab is the same functions and same interface as matplotlib, yet does something different that I don't know about. – Jason S Apr 10 at 20:09
1  
pyplot/pylab api: matplotlib.org/api/pyplot_api.html . The primary thing it adds is state-machine like behavior so you can use it like MATLAB. Most of it's functions figure out what you current axes is, and then call the correct function on that axes. – tcaswell Apr 10 at 20:12
show 4 more comments

1 Answer

up vote 0 down vote accepted

Huh. Per the discussion in comments from tcaswell, I changed

self.figure = pl.figure()

to

self.figure = matplotlib.figure.Figure()

and it fixed the problem. (python exits when I close the window)

share|improve this answer
1  
pl.figure() creates a matplotlib.figure.Figure() instance + sets up a gui object to handle it. That extra functionality is super nice when you are playing around with plots interactively, but can mess you up if you are embedding or doing stuff programatically. – tcaswell Apr 10 at 20:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.