Search code examples
pythonuser-interfacewxpythonsplitter

Running function in split windows in wxpython


I'm trying to use wx.SplitterWindow to split my main window into three pieces: one top window and two bottom windows (the bottom windows are left bottom and right bottom).

Here is what I have and what I am experiencing:

from __future__ import print_function
from numpy import arange, sin, pi
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wxmplot
import wx
import os

class topPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        #self.SetBackgroundColour("red")

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

    def plotWaves(self,parent):
        self.figureWave = Figure()
        self.axes = self.figureWave.add_subplot(111,aspect='equal')
        self.canvas = FigureCanvas(self, -1, self.figureWave)
        t = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
        s = [0.0, 1.0, 0.0, 1.0, 0.0, 2.0, 1.0, 2.0, 1.0, 0.0]
        self.axes.plot(t, s)
        self.sizerWave = wx.BoxSizer(wx.HORIZONTAL)
        self.sizerWave.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizerWave)
        self.Fit()

########################################################################

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

    def plotMAW(self,parent):

        self.figureMAW = Figure()
        self.axes = self.figureMAW.add_subplot(111,aspect='equal')
        self.canvas = FigureCanvas(self, -1, self.figureMAW)
        #self.axes.plot(self.waves)
        t = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
        s = [0.0, 1.0, 0.0, 1.0, 0.0, 2.0, 1.0, 2.0, 1.0, 0.0]
        self.axes.plot(t, s)
        self.sizerMAW = wx.BoxSizer(wx.HORIZONTAL)
        self.sizerMAW.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizerMAW)
        self.Fit()


########################################################################

### SETUP GUI HERE
 class myGUI(wx.Frame):
    def __init__(self):
        self.main = wx.Frame.__init__(self, None, wx.ID_ANY, "myGUI", size=(850,500))

        # DEFINE A SPLIT WINDOW FOR PLOTTING
        self.splitWin = wx.SplitterWindow(self)
        topP = topPanel(self)
        bottomPLeft = bottomLeft(self.splitWin)
        bottomPRight = bottomRight(self.splitWin)

        # SPLIT THE WINDOW
        self.splitWin.SplitVertically(bottomPLeft, bottomPRight)
        self.splitWin.SetMinimumPaneSize(350)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(topP, 1, wx.EXPAND)
        sizer.Add(self.splitWin, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.control = p = wx.Panel(self, 1, style=wx.TE_MULTILINE)

        # SETUP CONFIG WINDOW
        self.result = wx.StaticText(p, label="")
        self.result.SetForegroundColour(wx.RED)
        self.lblname = wx.StaticText(self, label="Config File:")
        self.editname = wx.TextCtrl(self, size=(140, -1))

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(5, 5)
        self.sizer.Add(self.lblname, (1, 2))
        self.sizer.Add(self.editname, (1, 3))

        # SETUP RUN MENU
        runMenu = wx.Menu()  # create a menu for running DAQ
        runConfig = runMenu.Append(wx.ID_ANY, "&Configure", "Load configuration file")
        menuBar = wx.MenuBar()
        menuBar.Append(runMenu, "&Run")
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        self.Bind(wx.EVT_MENU, self.onConfigure, runConfig)

        self.Show(1)
        self.aboutme = wx.MessageDialog( self, " A sample editor \n"
                            " in wxPython","About Sample Editor", wx.OK)
        self.doiexit = wx.MessageDialog( self, " Exit - R U Sure? \n",
                        "GOING away ...", wx.YES_NO)
        self.dirname = ''
    def onConfigure(self,e):
    # Load specific configuration file for system being used

        openFileDialog = wx.FileDialog(self, "Open", "", "",
                                   "Python files (*.py)|*.py",
                                   wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        if openFileDialog.ShowModal() == wx.ID_OK:
            self.filename=openFileDialog.GetFilename()
            self.dirname=openFileDialog.GetDirectory()
            filehandle=open(os.path.join(self.dirname, self.filename),'r')
            self.configName = self.filename[:-3]
            filehandle.close()
        lP = bottomLeft(self)
        lP.plotWaves(self)
        rP = bottomRight(self)
        rP.plotMAW(self)
        self.editname.AppendText(self.configName)
        openFileDialog.Destroy()

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = myGUI()
    frame.Show()
    app.MainLoop()

This is a complete example of my GUI and if you run the configure menu and load any file (it doesn't matter because that is just to run the def plotWaves and def plotMAW functions) you will see that the plots overtake the GUI. If you comment out the lines def plotWaves(self,parent): and def plotMAW(self,parent): then the plots appear in the correct place. The first image shows the result if you comment out the definition lines. The second image shows the result if you do NOT comment them out.

enter image description here

enter image description here


Solution

  • I think that you trying to overwrite the items that are already loaded into the splitter window.
    Try this instead:
    Note: I have no idea what wxmplot is, so I've commented it out

    from __future__ import print_function
    from numpy import arange, sin, pi
    import matplotlib
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
    from matplotlib.backends.backend_wx import NavigationToolbar2Wx
    from matplotlib.figure import Figure
    #import wxmplot
    import wx
    import os
    
    class topPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
            #self.SetBackgroundColour("red")
    
    class bottomLeft(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
    
        def plotWaves(self):
            self.figureWave = Figure()
            self.axes = self.figureWave.add_subplot(111,aspect='equal')
            self.canvas = FigureCanvas(self, -1, self.figureWave)
            t = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
            s = [0.0, 1.0, 0.0, 1.0, 0.0, 2.0, 1.0, 2.0, 1.0, 0.0]
            self.axes.plot(t, s)
            self.sizerWave = wx.BoxSizer(wx.HORIZONTAL)
            self.sizerWave.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.SetSizer(self.sizerWave)
            self.Fit()
    
    ########################################################################
    
    class bottomRight(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
    
        def plotMAW(self):
    
            self.figureMAW = Figure()
            self.axes = self.figureMAW.add_subplot(111,aspect='equal')
            self.canvas = FigureCanvas(self, -1, self.figureMAW)
            #self.axes.plot(self.waves)
            t = [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
            s = [0.0, 1.0, 0.0, 1.0, 0.0, 2.0, 1.0, 2.0, 1.0, 0.0]
            self.axes.plot(t, s)
            self.sizerMAW = wx.BoxSizer(wx.HORIZONTAL)
            self.sizerMAW.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.SetSizer(self.sizerMAW)
            self.Fit()
    
    
    ########################################################################
    
    ### SETUP GUI HERE
    class myGUI(wx.Frame):
        def __init__(self):
            self.main = wx.Frame.__init__(self, None, wx.ID_ANY, "myGUI", size=(850,500))
    
            # DEFINE A SPLIT WINDOW FOR PLOTTING
            self.splitWin = wx.SplitterWindow(self)
            topP = topPanel(self)
            self.lp = bottomLeft(self.splitWin)
            self.rp = bottomRight(self.splitWin)
    
            # SPLIT THE WINDOW
            self.splitWin.SplitVertically(self.lp, self.rp)
            self.splitWin.SetMinimumPaneSize(350)
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(topP, 1, wx.EXPAND)
            sizer.Add(self.splitWin, 1, wx.EXPAND)
            self.SetSizer(sizer)
    
            self.control = p = wx.Panel(self, 1, style=wx.TE_MULTILINE)
    
            # SETUP CONFIG WINDOW
            self.result = wx.StaticText(p, label="")
            self.result.SetForegroundColour(wx.RED)
            self.lblname = wx.StaticText(self, label="Config File:")
            self.editname = wx.TextCtrl(self, size=(140, -1))
    
            # Set sizer for the panel content
            self.sizer = wx.GridBagSizer(5, 5)
            self.sizer.Add(self.lblname, (1, 2))
            self.sizer.Add(self.editname, (1, 3))
    
            # SETUP RUN MENU
            runMenu = wx.Menu()  # create a menu for running DAQ
            runConfig = runMenu.Append(wx.ID_ANY, "&Configure", "Load configuration file")
            menuBar = wx.MenuBar()
            menuBar.Append(runMenu, "&Run")
            self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
            self.Bind(wx.EVT_MENU, self.onConfigure, runConfig)
    
            self.Show(1)
            self.aboutme = wx.MessageDialog( self, " A sample editor \n"
                                " in wxPython","About Sample Editor", wx.OK)
            self.doiexit = wx.MessageDialog( self, " Exit - R U Sure? \n",
                            "GOING away ...", wx.YES_NO)
            self.dirname = ''
    
        def onConfigure(self,e):
        # Load specific configuration file for system being used
    
            openFileDialog = wx.FileDialog(self, "Open", "", "",
                                       "Python files (*.py)|*.py",
                                       wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
            if openFileDialog.ShowModal() == wx.ID_OK:
                self.filename=openFileDialog.GetFilename()
                self.dirname=openFileDialog.GetDirectory()
                filehandle=open(os.path.join(self.dirname, self.filename),'r')
                self.configName = self.filename[:-3]
                filehandle.close()
            self.lp.plotWaves()
            self.rp.plotMAW()
            self.editname.AppendText(self.configName)
            openFileDialog.Destroy()
            self.Refresh()
    
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = myGUI()
        frame.Show()
        app.MainLoop()
    

    enter image description here