0

我用 Python 写了一些代码。

#!/usr/bin/python2
import wx
import wx.grid


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Manager")
        sizer = wx.GridBagSizer(hgap=0, vgap=0)

        overall = wx.grid.Grid(self)
        overall.CreateGrid(5,2)
        sizer.Add(overall, pos=(0,0), span=(5,2), flag=wx.EXPAND)

        self.SetSizer(sizer)
        self.Fit()

app = wx.PySimpleApp()
MainFrame().Show()

app.MainLoop()

它将显示一个 5x2 网格。但如果我改变:

sizer.Add(overall, pos=(0,0), span=(5,2), flag=wx.EXPAND)

进入:

sizer.Add(overall, pos=(0,0), span=(4,2), flag=wx.EXPAND)

什么都不会改变:(
但我想把一个 5x2 的网格放入一个 GridBagSizer 中,它有 4x2 的空间和一个滚动条。

怎么做???

4

1 回答 1

0

GridBagSizer 实际上用于将多个小部件放入类似网格的形状中,例如当您有一个带有成对标签和文本控件的表单时。在大多数情况下,它不仅仅适用于一个小部件。

您应该使用 BoxSizer。无论如何,我发现它们更灵活。wx.PySimpleApp 也已弃用。请改用 wx.App(False)。这是一些有效的代码:

import wx
import wx.grid


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Manager")
        sizer = wx.BoxSizer(wx.VERTICAL)

        overall = wx.grid.Grid(self)
        overall.CreateGrid(5,2)
        sizer.Add(overall, 0, flag=wx.EXPAND)

        self.SetSizer(sizer)
        self.Fit()

app = wx.App(False)
MainFrame().Show()

app.MainLoop()

编辑(2012 年 7 月 16 日):这是另一个更符合您的描述的示例:

import wx

########################################################################
class ChartPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        chart = ChartPanel(self)
        chart.SetBackgroundColour("blue")

        # create some sizers
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        topSizer = wx.BoxSizer(wx.HORIZONTAL)
        # change to VERTICAL if the buttons need to be stacked
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        for i in range(3):
            btn = wx.Button(self, label="Button #%s" % (i+1))
            btnSizer.Add(btn, 0, wx.ALL, 5)

        # put the buttons next to the Panel on the top
        topSizer.Add(btnSizer, 0, wx.ALL, 5)
        topSizer.Add(chart, 1, wx.EXPAND)

        mainSizer.Add(topSizer, 1, wx.EXPAND)
        mainSizer.AddSpacer(150,150)
        self.SetSizer(mainSizer)

########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="BoxSizer Example")
        panel = MainPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()
于 2012-07-13T13:21:42.167 回答