运行 wxpython 中拆分 windows 中的函数

Running function in split windows in wxpython

我正在尝试使用 wx.SplitterWindow 将我的主要 window 分成三部分:一个顶部 window 和两个底部 windows(底部 windows是左下角和右下角)。

这是我所拥有的和我正在经历的:

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()

这是我的 GUI 的完整示例,如果您 运行 配置菜单并加载任何文件(没关系,因为那只是 运行 def plotWaves 和 def plotMAW函数)你会看到绘图超过了 GUI。如果您注释掉 def plotWaves(self,parent):def plotMAW(self,parent): 行,则绘图会出现在正确的位置。如果您注释掉定义行,第一个图像将显示结果。如果您不注释掉它们,第二张图片显示结果。

我认为您试图覆盖已加载到拆分器中的项目 window。
试试这个:
注意:我不知道 wxmplot 是什么,所以我把它注释掉了

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()