Search code examples
pythonmatplotlibwxpython

Ploting in a wxFrame, How to pass an argument?


As example, consider the code in http://matplotlib.org/examples/user_interfaces/embedding_in_wx4.html . But, supose that I need to pass the amplitude as argument, how can I do that? if a modify the App class declaration like

class App(wx.App):
    def __init__(self,amplitude):
        wx.App.__init__(self)
        self.arg=amplitude
    def OnInit(self):
        '''Create the main window and insert the custom frame'''
        frame = CanvasFrame(self.arg)
        frame.Show(True)

        return True

and if I modify the CanvasFrame.__init_ _() to acept an argument this doesn't work.

Thanks for your help!


Solution

  • I do not understand why argument passing to CanvasFrame should not work. The linked mpl wx4 demo was modified as follows and it worked: EDIT II: Your error was swapping wx.App.__init__(self) and self.args = amplitude. In your case self.args was not set yet when App.OnInit(…) gets called.

    class CanvasFrame(wx.Frame):    
        def __init__(self, amplitude):
            wx.Frame.__init__(self, None, -1, …
            …
            self.amplitude = amplitude
            …
            # now use the amplitude
            s = sin(2*pi*t) * self.amplitude
    

    In derived App:

    class App(wx.App):
        def __init__(self, amplitude):
            self.amplitude = amplitude
            wx.App.__init__(self)
    
        def OnInit(self):
            'Create the main window and insert the custom frame'
            frame = CanvasFrame(self.amplitude)
            frame.Show(True)
    
            return True
    
    amplitude = 16
    app = App(amplitude)
    app.MainLoop()
    

    Probably it is not a good idea to have a CanvasFrame and App which cannot be initialised as a wx.Frame anymore (parent and title hardcoded into object), but this is another story.

    EDIT: extended example to fully meet OP's question (argument passing from the top level inward.)