在wxpython的拆分窗口中运行函数

2024-06-16 10:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用wx.SplitterWindow将我的主窗口分为三部分:一个顶部窗口和两个底部窗口(底部窗口是左下和右下)

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

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):行,则绘图显示在正确的位置。如果注释掉定义行,第一个图像将显示结果。第二幅图显示了不注释的结果

enter image description here

enter image description here


Tags: fromimportselfaddmatplotlibinitdefclass
1条回答
网友
1楼 · 发布于 2024-06-16 10:24:05

我认为您正在试图覆盖已加载到拆分器窗口中的项目。
试试这个:
注意:我不知道什么是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()

enter image description here

相关问题 更多 >