2

我是 wxpython 的新手,它似乎是构建 GUI 的非常强大的工具,但我有一个关于 GridBagSizer 的问题。您能否告诉我如何将放置在 GridBagSizer 中的项目的大小调整为它们应该放置的框架的大小。我在最后附上了示例:从 8 到 13 的按钮超出了框架,但我想要的是使按钮的大小自动调整大小以适应框架。任何意见和建议表示赞赏。

最好的问候,尼基塔

import wx

class Frame ( wx.Frame ):
   def __init__( self ):
      wx.Frame.__init__ ( self, None, id = wx.ID_ANY, title = wx.EmptyString, pos =                wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE & ~   (wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX|wx.RESIZE_BORDER) )
      nums = (('1',(0,3)), ('2',(2,3)), ('14', (3,3)), ('15',(4,3)), ('16',(5,3)), ('17',(6,3)), ('18',(7,3)),
        ('3',(1,0)), ('4',(1,1)), ('5',(1,2)), ('6',(1,3)), ('7',(1,4)), ('8',(1,5)), ('9',(1,6)), ('10',(1,7)),
        ('11',(1,8)), ('12',(1,9)), ('13',(1,10)))

      panel = wx.Panel(self,-1)
      panel.SetBackgroundColour("green")
      grid = wx.GridBagSizer(2,2)

      for num, pos in nums:
         button = wx.Button(panel, wx.NewId(), label = num)
         grid.Add(button, pos, flag=wx.EXPAND)

      bs2 = wx.BoxSizer(wx.VERTICAL)
      bs2.Add(grid, 0, wx.EXPAND,0)

      panel.SetSizer(bs2)
      panel.Layout()
      self.mainSizer = wx.BoxSizer(wx.VERTICAL)
      self.mainSizer.Add(panel, 0, wx.EXPAND, 0)
      self.SetSizer(self.mainSizer)
      self.Layout()

if __name__=='__main__':
   app=wx.PySimpleApp()
   app.frame = Frame()
   app.frame.Center()
   app.frame.Show(True)
   app.MainLoop()
4

1 回答 1

3

您必须通过调用允许列增长AddGrowableCol

  for i in range(11):
      grid.AddGrowableCol(i)

现在,它们可以增长,但它们不会缩小到超出初始大小!因此,您还应该减小它们的起始大小:

  for num, pos in nums:
     button = wx.Button(panel, wx.NewId(), size=(10,-1), label = num)
     #original code was:  button = wx.Button(panel, wx.NewId(), label = num)
     grid.Add(button, pos, flag=wx.EXPAND)

这是完整的清单:

import wx

class Frame ( wx.Frame ):
    def __init__( self ):
       wx.Frame.__init__ ( self, None, id = wx.ID_ANY, title = wx.EmptyString, pos =
                         wx.DefaultPosition, size = wx.Size( 500,300 ),
                         style = wx.DEFAULT_FRAME_STYLE & ~   (wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX|wx.RESIZE_BORDER) )
       nums = (('1',(0,3)), ('2',(2,3)), ('14', (3,3)), ('15',(4,3)), ('16',(5,3)), ('17',(6,3)), ('18',(7,3)),
         ('3',(1,0)), ('4',(1,1)), ('5',(1,2)), ('6',(1,3)), ('7',(1,4)), ('8',(1,5)), ('9',(1,6)), ('10',(1,7)),
         ('11',(1,8)), ('12',(1,9)), ('13',(1,10)))

       panel = wx.Panel(self,-1)
       panel.SetBackgroundColour("green")
       grid = wx.GridBagSizer(2,2)

       for num, pos in nums:
           button = wx.Button(panel, wx.NewId(), size=(10,-1), label = num)
           grid.Add(button, pos, flag=wx.EXPAND)

       for i in range(11):
           grid.AddGrowableCol(i)

       bs2 = wx.BoxSizer(wx.VERTICAL)
       bs2.Add(grid, 0, wx.EXPAND,0)

       panel.SetSizer(bs2)
       panel.Layout()
       self.mainSizer = wx.BoxSizer(wx.VERTICAL)
       self.mainSizer.Add(panel, 0, wx.EXPAND, 0)
       self.SetSizer(self.mainSizer)
       self.Layout()

if __name__=='__main__':
    app=wx.PySimpleApp()
    app.frame = Frame()
    app.frame.Center()
    app.frame.Show(True)
    app.MainLoop()
于 2012-10-16T08:12:29.340 回答