堆叠两个wx.Grid只调整第二个

时间:2014-05-29 03:09:41

标签: python python-2.7 wxpython wxwidgets

我正在尝试在同一帧中创建两个垂直堆叠的网格。 由于无法禁用滚动条网格,我正在尝试此转向 第一个网格必须占据框架的顶部,而第二个网格必须占据框架的顶部。当框架调整大小时,顶部网格必须始终保持可见而没有滚动条,而下部网格可以显示滚动条。 这是一个示例代码。唯一的问题是我无法使第一个网格仅占据框架中的大小,无论如何都占据了框架的一半。

import wx
import wx.grid as glib
import wx.lib.scrolledpanel as SP

# The main panel:        
class grids(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent)
        # scrolled panels
        resizableScrolledPanel = SP.ScrolledPanel(self)
        fixedScrolledPanel = SP.ScrolledPanel(self)
        # main sizer for everything:
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        # create grids
        fixGrid = glib.Grid(fixedScrolledPanel)
        resizableGrid = glib.Grid(resizableScrolledPanel)
        # customize grids
        fixGrid.CreateGrid(3,10)
        resizableGrid.CreateGrid(20,10)
        fixGrid.SetDefaultCellBackgroundColour( [176, 196, 222] )#light steel blue
        fixGrid.SetGridLineColour([0,0,0])
        [fixGrid.SetRowLabelValue(idx, ["row 1","row 2","row 3"][idx]) for idx in range(3)]
        resizableGrid.SetColLabelSize(0)
        fixGrid.SetRowLabelSize( wx.grid.GRID_AUTOSIZE )
        resizableGrid.SetRowLabelSize( fixGrid.GetRowLabelSize() )
        # Create the grids sizers
        propSizer = wx.BoxSizer(wx.VERTICAL)
        dataSizer = wx.BoxSizer(wx.VERTICAL)
        propSizer.Add(fixGrid,1,wx.RIGHT|wx.TOP|wx.EXPAND,5)
        dataSizer.Add(resizableGrid,1,wx.RIGHT|wx.BOTTOM|wx.EXPAND,5)
        fixedScrolledPanel.SetSizer(propSizer)
        resizableScrolledPanel.SetSizer(dataSizer)
        fixedScrolledPanel.Layout()
        resizableScrolledPanel.Layout()
        resizableScrolledPanel.SetupScrolling(scroll_x=True, scroll_y=True)
        fixedScrolledPanel.SetupScrolling(scroll_x=False, scroll_y=False)
        # add to main sizer
        mainSizer.Add(fixedScrolledPanel,1,wx.RIGHT|wx.TOP|wx.EXPAND,5)
        mainSizer.Add(resizableScrolledPanel,1,wx.RIGHT|wx.BOTTOM|wx.EXPAND,5)
        self.SetSizer(mainSizer)
        self.Fit()

        print fixedScrolledPanel.GetSize() # returns (0,0)

# The main frame:
class MainFrame(wx.Frame):
    def __init__(self,parent,title):
    wx.Frame.__init__(self,parent,-1,title=title, size=(850,500))
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    sizer.Add(grids(self),1,wx.EXPAND)
    self.SetSizer(sizer)
    self.SetAutoLayout(1)

app = wx.App(False)
frame = MainFrame(None, "Scroll Test")
frame.Show(True)
app.MainLoop()

通常我应该将fixedSizer比例设置为0,对于fixedScrolledPanel,就像这样

mainSizer.Add(fixedScrolledPanel,0,wx.RIGHT|wx.TOP|wx.EXPAND,5)

但是由于fixedScrolledPanel是(0,0)第一个网格消失...

2 个答案:

答案 0 :(得分:1)

缺少的细节是SetSizeHints电话; sizer需要知道要扩展到什么。您还希望mainSizer中的面板具有proportion = 0

import wx
import wx.grid as glib
import wx.lib.scrolledpanel as SP

# The main panel:        
class grids(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent)
        # scrolled panels
        resizableScrolledPanel = SP.ScrolledPanel(self)
        fixedScrolledPanel = SP.ScrolledPanel(self)
        # main sizer for everything:
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        # create grids
        fixGrid = glib.Grid(fixedScrolledPanel)
        resizableGrid = glib.Grid(resizableScrolledPanel)
        # customize grids
        fixGrid.CreateGrid(3,10)
        resizableGrid.CreateGrid(20,10)
        fixGrid.SetDefaultCellBackgroundColour( [176, 196, 222] )#light steel blue
        fixGrid.SetGridLineColour([0,0,0])
        [fixGrid.SetRowLabelValue(idx, ["row 1","row 2","row 3"][idx]) for idx in range(3)]
        resizableGrid.SetColLabelSize(0)
        fixGrid.SetRowLabelSize( wx.grid.GRID_AUTOSIZE )
        resizableGrid.SetRowLabelSize( fixGrid.GetRowLabelSize() )
        # Create the grids sizers
        fixedSizer  = wx.BoxSizer(wx.VERTICAL)
        resizeSizer = wx.BoxSizer(wx.VERTICAL)
        resizableScrolledPanel.SetupScrolling(scroll_x=True, scroll_y=True)
        fixedScrolledPanel.SetupScrolling(scroll_x=False, scroll_y=False)

        fixedSizer.Add\
        (
            fixGrid,
            proportion = 1,
            flag       = wx.EXPAND,
            border     = 0
        )
        fixedScrolledPanel.SetSizer(fixedSizer)
        fixedSizer.SetSizeHints(fixedScrolledPanel)
        mainSizer.Add\
        (
            fixedScrolledPanel,
            proportion = 0,
            flag       = wx.EXPAND | wx.RIGHT | wx.TOP,
            border     = 5
        )
        resizeSizer.Add\
        (
            resizableGrid,
            proportion = 1,
            flag       = wx.EXPAND,
            border     = 0
        )
        resizableScrolledPanel.SetSizer(resizeSizer)
        resizeSizer.SetSizeHints(resizableScrolledPanel)
        mainSizer.Add\
        (
            resizableScrolledPanel,
            proportion = 0,
            flag       = wx.EXPAND | wx.RIGHT | wx.BOTTOM,
            border     = 5
        )
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)

# The main frame:
class MainFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,-1,title=title, size=(850,500))
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(grids(self),1,wx.EXPAND)
        self.SetSizer(sizer)
        sizer.Fit(self)

app = wx.App(False)
frame = MainFrame(None, "Scroll Test")
frame.Show(True)
app.MainLoop()

为了便于阅读,我重新格式化了代码。

答案 1 :(得分:0)

@ dilbert 修复滚动条需要很少的修改

import wx
import wx.grid as glib
import wx.lib.scrolledpanel as SP

# The main panel:        
class grids(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self,parent)
        # scrolled panels
        resizableScrolledPanel = SP.ScrolledPanel(self)
        fixedScrolledPanel = SP.ScrolledPanel(self)
        # main sizer for everything:
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        # create grids
        fixGrid = glib.Grid(fixedScrolledPanel)
        resizableGrid = glib.Grid(resizableScrolledPanel)
        # customize grids
        fixGrid.CreateGrid(3,10)
        resizableGrid.CreateGrid(20,10)
        fixGrid.SetDefaultCellBackgroundColour( [176, 196, 222] )#light steel blue
        fixGrid.SetGridLineColour([0,0,0])
        [fixGrid.SetRowLabelValue(idx, ["row 1","row 2","row 3"][idx]) for idx in range(3)]
        resizableGrid.SetColLabelSize(0)
        fixGrid.SetRowLabelSize( wx.grid.GRID_AUTOSIZE )
        resizableGrid.SetRowLabelSize( fixGrid.GetRowLabelSize() )
        # Create the grids sizers
        fixedSizer  = wx.BoxSizer(wx.VERTICAL)
        resizeSizer = wx.BoxSizer(wx.VERTICAL)
        resizableScrolledPanel.SetupScrolling(scroll_x=True, scroll_y=True)
        fixedScrolledPanel.SetupScrolling(scroll_x=False, scroll_y=False)

        fixedSizer.Add\
        (
        fixGrid,
        proportion = 1,
        flag       = wx.EXPAND,
        border     = 0
        )
        fixedScrolledPanel.SetSizer(fixedSizer)
        fixedSizer.SetSizeHints(fixedScrolledPanel)

        mainSizer.Add\
        (
        fixedScrolledPanel,
        proportion = 0,
        flag       = wx.EXPAND | wx.RIGHT | wx.TOP,
        border     = 5
        )

        resizeSizer.Add\
        (
        resizableGrid,
        proportion = 1,
        flag       = wx.EXPAND,
        border     = 0
        )

        resizableScrolledPanel.SetSizer(resizeSizer)

        mainSizer.Add\
        (
        resizableScrolledPanel,
        proportion = 1,
        flag       = wx.EXPAND | wx.RIGHT | wx.BOTTOM,
        border     = 5
        )
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)

# The main frame:
class MainFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,-1,title=title, size=(850,500))
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(grids(self),1,wx.EXPAND)
        self.SetSizer(sizer)
        sizer.Fit(self)

app = wx.App(False)
frame = MainFrame(None, "Scroll Test")
frame.Show(True)
app.MainLoop()