使用步骤:
创建sizers布局管理器
调用SetSizer()将布局管理器添加到容器中
调用Add()将组件添加到布局管理器中
调用Fit()计算计算布局布局管理器和容器窗口的大小
#-*- coding:utf-8 -*-
import wx
class gridbagSizer(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,u"布局管理器之-表格",size = (500,500))
self.panel = wx.Panel(self,-1)
self.grid = wx.GridBagSizer(hgap= 5, vgap =5)
colorlist = [u'红',u'绿',u'蓝',u'黄']
col =0
for color in colorlist:
btn = wx.Button(self.panel,-1,color)
self.grid.Add(btn,pos = (0,col))
col = col + 1
btn = wx.Button(self.panel,-1,u'白')
self.grid.Add(btn,pos = (1,0),span = (1,3),flag = wx.EXPAND)
btn1 = wx.Button(self.panel,-1,u'紫')
self.grid.Add(btn1,pos = (2,1),span = (2,2),flag = wx.EXPAND)
self.panel.SetSizer(self.grid)
self.panel.Fit()
if __name__ == '__main__':
app = wx.PySimpleApp()
fram = gridbagSizer()
fram.Show()
app.MainLoop()
#-*- coding:utf-8 -*-
import wx
class MyBoxSizer(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,u"布局管理器之-表格",size = (500,500))
self.panel = wx.Panel(self,-1)
self.grid = wx.BoxSizer(wx.HORIZONTAL)
self.grid1 = wx.BoxSizer(wx.VERTICAL)
self.grid2 = wx.BoxSizer(wx.VERTICAL)
colorlist = [u'红',u'绿',u'蓝',u'黄']
col =0
for color in colorlist:
btn = wx.Button(self.panel,-1,color)
self.grid1.Add(btn)
for color in colorlist:
btn = wx.Button(self.panel,-1,color)
self.grid2.Add(btn)
self.grid.Add(self.grid1,flag=wx.EXPAND) #嵌套
self.grid.Add(self.grid2,flag=wx.EXPAND) #嵌套
self.panel.SetSizer(self.grid)
self.panel.Fit()
if __name__ == '__main__':
app = wx.PySimpleApp()
fram = MyBoxSizer()
fram.Show()
app.MainLoop()
#-*- coding:utf-8 -*-
import wx
class MyBoxSizer(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,u"布局管理器之-表格",size = (500,500))
self.panel = wx.Panel(self,-1)
self.grid = wx.BoxSizer(wx.HORIZONTAL)
self.grid1 = wx.BoxSizer(wx.VERTICAL)
self.grid2 = wx.BoxSizer(wx.VERTICAL)
colorlist = [u'红',u'绿',u'蓝',u'黄']
col =0
for color in colorlist:
btn = wx.Button(self.panel,-1,color)
self.grid1.Add(btn)
for color in colorlist:
btn = wx.Button(self.panel,-1,color)
self.grid2.Add(btn)
self.grid.Add(self.grid1,flag=wx.EXPAND) #嵌套
self.grid.Add(self.grid2,flag=wx.EXPAND) #嵌套
self.panel.SetSizer(self.grid)
self.panel.Fit()
if __name__ == '__main__':
app = wx.PySimpleApp()
fram = MyBoxSizer()
fram.Show()
app.MainLoop()