Search code examples
pythonwxpythonmatplotlibfigure

How can I open two different pylab figures from the same button from a wxpython gui?


I am new to python and I have a serious problem that I cannot overcome. I have created a gui using wxpython that has two text fields and a button. When this button is pressed I call a function that call another function which creates a pie figure according to the input in text boxes. The problem is that if the user do not close the figure and enter new values to the textboxes and press again the button, the program crash instead of showing a second figure. I tried to create different threads when the button is pressed the result is the same.

More specifically:

this are the functions that are called when the button is clicked:

def statistics_button(self,event):

   t=threading.Thread(target=self.m_button21OnButtonClick,args=(event,))
    t.start()
    print t    

def m_button21OnButtonClick( self, event ):

    '''some code here'''


    fig=statistics.mmr_dist(mmr1,mmr2) 

    show()

the statistics_button is called first and then this calls the m_button21OnButtonClick. the statistics.mmr_dist function is the following:

'''some code'''
fig=pylab.figure(tit,figsize=(8,8),frameon=False)

ax=pylab.axes([0.1, 0.1, 0.8, 0.8])
pita=pylab.pie(db.values(), labels=tuple(db.keys()), autopct='%1.1f%%', shadow=True)
ti="%s-%s\n Maximum Distance = %s m\n Minimum Distance = %s m" % (ddf1.label,ddf2.label,maxdist,mindist)
title(ti, bbox={'facecolor':'0.8', 'pad':5}) 


'''some code'''

return fig

So far I have understood that the show() command blocks the function m_button21OnButtonClick from finishing so it cannot be called again when the button is clicked unless the figure is closed. But this is the reason I implemented different threads. Though it doesn't seem to work.


Solution

  • See this page for advice on getting pylab to work with wxPython--which you probably shouldn't really try (see next paragraph). The problem is that pylab uses Tkinter which is incompatible with a running copy of wxPython.

    Ultimately, you should just embed your plots in wxPython. That works very well and is a better user experience anyway.