2012-12-18 41 views
2

我正在使用wxpython GUI。我有三个面板(左边的两个水平分割,右边的一个与另外两个垂直分割 - 认为左边的两个方格和右边的一个高大的矩形)。我正在尝试在面板3(图像中的P3)中安装Gridsizer。我似乎无法让网格面板“填充”整个面板3.相反,它们捕捉到顶部和底部。理想情况下,我想要10个小面板(2行,5列)调整大小并很好地填充面板3(所有尺寸相同,尺寸足以查看每个面板)。我究竟做错了什么?谢谢!wxpython Gridsizer不适合面板

**** ****编辑我 已经改变了我的代码如下所示:

 sizer_31 = wx.BoxSizer(wx.VERTICAL) 
     gs = wx.GridSizer(0,4,7,7) 

     for i in self.Panel_Others.keys(): 
      gs.Add(self.Panel_Others[i],0,wx.ALIGN_CENTER|wx.ALL,5) 

     sizer_31.Add(gs,0,wx.ALIGN_CENTER|wx.ALL,5) 
     self.OtherTeams.SetSizer(sizer_31) 
     sizer_31.SetSizeHints(self.OtherTeams) 

而且我的新面板3看起来像下面的图片。这是一些改进。但是,我希望单个面板(总共10个)扩大相同的数量,以便整个笔记本页面覆盖相同尺寸的较小面板(想象日历的外观和每天是面板)。

enter image description here

****编辑完****

下面是我的原代码:

注:self.OtherTeamsself.Panel3下wx.Notebook页。 self.Panel_Others是一个字典,其中包含我正在显示的wx.Panels(这是动态更改的,所以这就是为什么我有它们的字典而不是指定它们)。

 sizer_31 = wx.BoxSizer(wx.HORIZONTAL) 
     gs = wx.GridSizer(2,5,5,5) 

     for i in self.Panel_Others.keys(): 
      sizer_temp = wx.BoxSizer(wx.VERTICAL) 
      sizer_temp.Add(self.Panel_Others[i],1,wx.EXPAND) 
      gs.Add(sizer_temp,1,wx.EXPAND,0) 

     sizer_31.Add(gs,0,wx.EXPAND) 
     self.OtherTeams.SetSizer(sizer_31) 

enter image description here

回答

1

wxPython 2.8这将导致在页面的左上角一个模糊。 wxPython 2.9对sizer来说有点聪明,这就是为什么它看起来有点奏效。

首先,您在BoxSizer内创建GridSizer,但不允许它动态扩展(wx.EXPAND)。 第二,您将sizer设置为wx.Notebook而不是页面(面板),该页面会将整个布局向下移动并裁切网格尺寸器中控件/面板的底部行,或者在2.8中因此将被完全忽略一切都在左上角。

这应该做的伎俩:

sizer_31 = wx.BoxSizer(wx.VERTICAL) # page (panel) sizer for outer border 
gs = wx.GridSizer(0,4,7,7) # content sizer 

for i in self.Panel_Others.keys(): 
    gs.Add(self.Panel_Others[i],0,wx.EXPAND|wx.ALL,5) 
    # + wx.EXPAND: allow the content to fill the 'cells' 
    #    (proportion is ignored here so it can be 0) 
sizer_31.Add(gs,1,wx.EXPAND|wx.ALL,5) 
# + 1, wx.EXPAND: allow the grid to expand to the main sizer 
#     which in turn fits to the page 
self.Panel3.SetSizer(sizer_31) # set the sizer to page, not the notebook 

编辑:下面的例子演示了如何动态地在wx.Notebook页面更新wx.GridSizer的内容:

import wx 

class MyFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, title='wx.Notebook') 
     # create a wx.Notebook 
     book = wx.Notebook(self) 
     # create a new page; wx.Notebook owns the page 
     # therefore the notebook must be the parent window 
     page = MyPage(parent=book) 
     # add the page to the notebook: 
     book.AddPage(page, 'Page 1') 
     book.AddPage(MyPage(book),'Page 2') 
     book.AddPage(MyPage(book),'Page 3') 

class MyPage(wx.Panel): 
    "a page for wx.Notebook" 
    def __init__(self, parent): 
     # NOTE: wxPython 2.9.4/winXP may create graphic 
     # artifacts when more than one page is added. This can 
     # be avoided by creating the page with a size of (0,0). 
     # the reason for this is: the page (here a panel) is 
     # created with the default size and position (upper 
     # left corner). Once the layout is applied, the page 
     # is moved to the proper coordinates. However, the 
     # wx.Notebook is not redrawn so it will leave a black 
     # spot where the page was moved away from. Alternatively, 
     # the frame or notebook can be Refresh()ed after the 
     # frame is shown. 
     wx.Panel.__init__(self, parent, size=(0,0)) 
     # create sizer to add a border 
     pageSizer = wx.BoxSizer(wx.VERTICAL) 
     # create a grid sizer for the content 
     self.grid = wx.GridSizer(0, 4, 5, 5) 
     # add the grid sizer to the page sizer: 
     # to allow the grid to fill the entire page, 
     # set proportion>0 and add the wx.EXPAND flag. 
     # the border of 10 is the spacing between the 
     # page and the cells of the grid 
     pageSizer.Add(self.grid, 1, wx.EXPAND|wx.ALL, 10) 
     # set the main sizer: 
     self.SetSizer(pageSizer) 
     # add content to the grid 
     self.GenerateContent() 
     # DEMO: click any white space on the page to 
     # generate new content 
     self.Bind(wx.EVT_LEFT_DOWN, self.GenerateContent) 

    def GenerateContent(self, event=None): 
     "dynamically create page content" 
     # remove all items from the grid and destroy them 
     # NOTE: if you don't actually create new controls here, 
     # but use a list of controls created elsewhere (like in 
     # in your code), do not delete the items! Use: 
     # deleteWindows=False instead. 
     self.grid.Clear(deleteWindows=True) 
     # generate new grid content 
     for i in range(10): 
      # the contents are owned by the page, 
      # therefore the page must be the parent window: 
      # NOTE: size=(0,0) here is not required but 
      # eliminates a briefly shown graphic artifact 
      cell = wx.Panel(self, size=(0,0)) 
      cell.SetBackgroundColour(self.NewColor()) 
      # add the content (cells) to the grid: 
      # specify the wx.EXPAND flag to fit the 
      # content to the grid cell 
      self.grid.Add(cell, flag=wx.EXPAND) 
     # DEMO: hint text 
     self.grid.Add(wx.StaticText(self, label= 
      'Click empty cell to update content ->'), 
         flag=wx.EXPAND) 
     # recalculate the layout to move the new contents 
     # of the grid sizer into place 
     self.Layout() 

    col = 0 # DEMO: coloring 
    def NewColor(self): 
     self.col = (self.col + 3) % 241 
     return wx.Colour(0, self.col * 10 % 255, self.col % 255) 

app = wx.App(False) 
MyFrame().Show() 
app.MainLoop()