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