垂直拆分wxpython中的底部窗口

2024-06-16 10:20:34 发布

您现在位置: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示例,如果您运行configure菜单并加载任何文件(这无关紧要,因为这只是运行def plotWaves和def plotMAW函数),您将看到绘图超过GUI。如果注释掉def plotWaves(self,parent):def plotMAW(self,parent):这两行,那么绘图将出现在正确的位置。如果注释掉定义行,第一个图像将显示结果。第二幅图显示了如果不将它们注释掉的结果。在

enter image description here

enter image description here


Tags: fromimportselfaddmatplotlibinitdefclass
2条回答

会是这样吗?在

import wx


class MainGUI(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent=None, size=(750,500))
        self.Centre()

        topPanel = TopPanel(self)
        hLeftPanel = LeftPanel(self)
        hRightPanel = RightPanel(self)

        hBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
        hBoxSizer.Add(hLeftPanel, 1, wx.EXPAND)
        hBoxSizer.Add(hRightPanel, 1, wx.EXPAND)        

        vBoxSizer = wx.BoxSizer(wx.VERTICAL)
        vBoxSizer.Add(topPanel, 1, wx.EXPAND)
        vBoxSizer.Add(hBoxSizer, 1, wx.EXPAND)

        self.SetSizer(vBoxSizer)


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


class LeftPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour("blue")


class RightPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour("green")


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

我希望我帮过忙!在

您没有定义两个面板来组成显示屏的底部。
这就是你要找的:

import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class topPanel(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        Text=wx.StaticText(self, -1, ("Top Panel"))

class bottomRight(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        Text=wx.StaticText(self, -1, ("Bottom Right"))

class bottomLeft(wx.Panel):
    # PANEL FOR PLOTTING DATA AS IT COMES IN
    #                                   
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        Text = wx.StaticText(self,-1,"Bottom Left")
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111,aspect='equal')
        self.canvas = FigureCanvas(self, -1, self.figure)
        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.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(Text, 0,0,0,0)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

class myGUI(wx.Frame):
    def __init__(self):
        self.main = wx.Frame.__init__(self, None, wx.ID_ANY, "myGUI", size=(750,500))
        # DEFINE A SPLIT WINDOW
        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)

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

enter image description here

相关问题 更多 >