如何使用滚动条将wx.grid放入GridBagSizer中

时间:2012-07-13 09:32:35

标签: python user-interface wxpython

我在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网格放入一个带有4x2空间的GridBagSizer和一个滚动条。

怎么做???

1 个答案:

答案 0 :(得分: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()

编辑(7/16/12):这是另一个更符合您描述的示例:

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