使用BoxSizer的多个面板

时间:2019-04-19 12:33:40

标签: python wxpython wxwidgets

我正在尝试将一个面板分成两个面板。 因此,Button1应该在一个面板中,而Button 2、3、4应该在另一面板中。

这是我的可运行代码,无法正确显示面板。

import wx
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure


class Frame1(wx.Frame):

    def __init__(self, parent):

        wx.Frame.__init__(self, None,
                          pos=wx.Point(0, 0),size=wx.Size(800, 200),
                          style=wx.DEFAULT_FRAME_STYLE | wx.MAXIMIZE )

        self.panel1 = wx.Panel(self)
        self.panel2 = wx.Panel(self)
        #self.panel1.SetBackgroundColour('blue')
        self.panel3 = wx.Panel(self)
        self.panel4 = wx.Panel(self)
        self.panel3.SetBackgroundColour('red')


        sizer1 = wx.BoxSizer(wx.VERTICAL)
        sizer1.Add(self.panel4, 1, wx.EXPAND)
        sizer1.Add(self.panel1, 1, wx.EXPAND)
        self.SetSizer(sizer1)
        sizer1.Fit(self)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(sizer1, 1, wx.EXPAND)
        sizer.Add(self.panel2, 1, wx.EXPAND)
        sizer.Add(self.panel3, 1, wx.EXPAND)
        self.SetSizer(sizer)
        #sizer.Fit(self)


        self.log = wx.TextCtrl(self.panel3, style=wx.TE_RICH|wx.TE_MULTILINE)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.log, 1, wx.EXPAND|wx.ALL, 15)
        self.panel3.SetSizer(sizer)

        emptycell = (0,0)
        self.button1 = wx.Button(self.panel4, -1, "Button1", size=wx.Size(150, 70))
        self.button2 = wx.Button(self.panel1, -1, "Button2")
        self.button3 = wx.Button(self.panel1, -1, "Button3")
        self.button4 = wx.Button(self.panel1, -1, "Button4")

        sizer = wx.GridSizer(8,3,0,0)

        sizer.Add(emptycell, 0, wx.EXPAND)
        sizer.Add(self.button1, 0, wx.ALIGN_CENTER|wx.LEFT, 10)
        sizer.Add(emptycell, 0, wx.EXPAND)
        sizer.Add(self.button2, 0, wx.ALIGN_CENTER)
        sizer.Add(self.button3, 0, wx.ALIGN_CENTER)
        sizer.Add(self.button4, 0, wx.ALIGN_CENTER)
        self.panel1.SetSizer(sizer)



        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.panel2, -1, self.figure)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.canvas, 0, wx.EXPAND)
        self.panel2.SetSizer(sizer)
        #self.panel2.Fit()


        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)

        #self.Center()


#Every wxWidgets application must have a class derived from wxApp
class MyApp(wx.App):
    # wxWidgets calls this method to initialize the application
    def OnInit(self):

        # Create an instance of our customized Frame class
        self.frame = Frame1(None)
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True


if __name__ == '__main__':
    application = MyApp(0)
    application.MainLoop()

所需结果

enter image description here

实际结果

enter image description here

2 个答案:

答案 0 :(得分:2)

我不得不修改一下,但是尝试给每个面板一个自己的大小调整器,并在最后将它们组合在一起。另外,请使用描述性名称,否则它很快就会变成一团糟,很难理解。

import wx
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure


class Frame1(wx.Frame):

    def __init__(self, parent):

        wx.Frame.__init__(self, None,
                          pos=wx.Point(0, 0),size=wx.Size(800, 200),
                          style=wx.DEFAULT_FRAME_STYLE | wx.MAXIMIZE )

        self.panel1_1 = wx.Panel(self)
        self.panel1_2 = wx.Panel(self)
        self.panel2 = wx.Panel(self)
        self.panel3 = wx.Panel(self)
        self.panel3.SetBackgroundColour('red')
        self.panel1_1.SetBackgroundColour('green')
        self.panel1_2.SetBackgroundColour('blue')


        self.log = wx.TextCtrl(self.panel3, style=wx.TE_RICH|wx.TE_MULTILINE)
        log_sizer = wx.BoxSizer(wx.HORIZONTAL)
        log_sizer.Add(self.log, 1, wx.EXPAND|wx.ALL, 15)
        self.panel3.SetSizer(log_sizer)

        self.button1 = wx.Button(self.panel1_1, -1, "Button1", size=wx.Size(150, 70))

        button_sizer1 = wx.GridSizer(1,3,0,0)
        button_sizer1.Add((0,0), 0, wx.ALL, 10)
        button_sizer1.Add(self.button1, 0, wx.ALL, 10)
        button_sizer1.Add((0,0), 0, wx.ALL, 10)
        self.panel1_1.SetSizer(button_sizer1)

        self.button2 = wx.Button(self.panel1_2, -1, "Button2")
        self.button3 = wx.Button(self.panel1_2, -1, "Button3")
        self.button4 = wx.Button(self.panel1_2, -1, "Button4")

        button_sizer2 = wx.GridSizer(1,3,0,0)
        button_sizer2.Add(self.button2, 0, wx.ALL|wx.ALIGN_LEFT, 10)
        button_sizer2.Add(self.button3, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 10)
        button_sizer2.Add(self.button4, 0, wx.ALL|wx.ALIGN_RIGHT, 10)
        self.panel1_2.SetSizer(button_sizer2)

        main_button_sizer = wx.BoxSizer(wx.VERTICAL)
        main_button_sizer.Add(self.panel1_1)
        main_button_sizer.Add(self.panel1_2, 1, wx.EXPAND)

        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.panel2, -1, self.figure)

        fig_sizer = wx.BoxSizer(wx.HORIZONTAL)
        fig_sizer.Add(self.canvas, 0, wx.EXPAND)
        self.panel2.SetSizer(fig_sizer)

        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.Add(main_button_sizer, 1, wx.EXPAND)
        main_sizer.Add(self.panel2, 1, wx.EXPAND)
        main_sizer.Add(self.panel3, 1, wx.EXPAND)
        self.SetSizer(main_sizer)
        self.Layout()

        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)


#Every wxWidgets application must have a class derived from wxApp
class MyApp(wx.App):
    # wxWidgets calls this method to initialize the application
    def OnInit(self):

        # Create an instance of our customized Frame class
        self.frame = Frame1(None)
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True


if __name__ == '__main__':
    application = MyApp(0)
    application.MainLoop()

enter image description here

答案 1 :(得分:0)

Oof,其中一些panel和sizer变量名称很难遵循。

在处理wx时,最好跟踪哪些窗口(例如面板,按钮等)将位于其他窗口之内,并保持警惕,以了解如何声明父对象。每个。

例如,如果您有一个包含多个元素的面板(例如panel1),则在实例化这些元素时,将其父元素都设置为panel1。例如,创建self.button1时,将其父级设置为panel1(与其他按钮相同),因为您将它们都放入panel1中。

这是因为wx会自然尝试在其父母内部安装窗户。当一个窗口的父元素是一个元素,但是一个大小调整程序试图将它放在另一个元素中时,事情就变得很麻烦了。很难完全遵循,但我想这就是您的问题所在。

使您的代码(a)更易于阅读/关注/调试,以及(b)更好地工作的另一件事是:不要害怕实例化多个sizer变量。更好的是,给他们起与他们正在组织的小组相匹配的名字!

我发现将__init__函数分为三个部分很有帮助(这也可能对您有用):(1)从“顶层”实例化所有您需要的窗口/对象向下,为其分配正确的父窗口; (2)实例化所有的大小调整器,每个窗口包含其他元素; (3)从“底层”开始,将窗口添加到其父级大小调整器,在每个父级面板上调用SetSizer,然后进行处理,直到为顶级窗口设置了主大小调整器为止。

如果这对您不可行,请随时向我发送消息,我们将看看是否可以解析出wx无法正常运行的原因。