wxPython frame的布局详细解释(一)

    对于初学者,wxPython的布局定位是有点麻烦,需要仔细查看官方文档。(原文: wiki.wxpython.org/learnSizer3.py
   
    以下是我学习过程中实现的实例,以便与大家共同交流:)
   
1. wx.BoxSizer() 在wxPython定位构件程中使用最多最直观的sizer.

     原型:

 
  1. wx.BoxSizer.__init__(self, orient)  
  2.   
  3. sizer = wx.BoxSizer(wx.HORIZONTAL)   #创建一个水平方向的box ; wx.VERTICAL ,垂直方向的box  

   它还有以下常用到的方法:

 
  1. Add(self, item, proportion, flag, border, userData)  
  2. Insert(self, before, item, proportion, flag, border, userData, realIndex)  
  3. Layout(self)  
  4. Prepend(self, item, proportion, flag, border, userData)  
  5. Remove(self, indx, pop)  
  6. Show(self, item, show) 
  7. .
  8. .
  9. .
  10. (原文: http://www.wxpython.org/docs/api/wx.BoxSizer-class.html)

   实例1:

 
  1. def __init__(self, parent):  
  2.         wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)  
  3.           
  4.         b = 5  
  5.         vsizer1 = wx.BoxSizer(orient=wx.VERTICAL)  
  6.         wbtn1 = wx.Button(self, -1, 'Botton1')  
  7.         wbtn2 = wx.Button(self, -1, 'Botton2')   
  8.         vsizer1.Add(item=wbtn1, proportion=1, flag=wx.EXPAND|wx.ALL, border=b)  
  9.         vsizer1.Add(wbtn2, 0, wx.ALIGN_CENTER | wx.ALL, b)
  10.         # wx.ALIGN_LEFT, wx.ALIGN_RIGHT  
  11.         self.SetSizer(vsizer1)  

这里需要说明的参数: proportion表示的wbtn1所占vsizer1垂直方向的比例;flag表示的wbtn1是怎样定位的(包括对齐方式、边框有无、是否扩展、是否拉伸等等),个人认为,定位主要体现在这两个参数的设置。(以下的几种方式的参数与此类同)

2. wx.FlexGridSizer()主要用于规则的排列构件时使用的(eg:Execel)

原型:

#构造函数 
# rows/cols表示构成的行数与列数,vgap/hgap表示管理构件时垂直与水平之间的间隔。
__init__(self, rows=1, cols=0, vgap=0, hgap=0)   (Constructor)      Constructor for a wx.FlexGridSizer. rows and cols determine the number of columns and rows in the sizer - if either of the parameters is zero, it will be calculated to from the total number of children in the sizer, thus making the sizer grow dynamically. vgap and hgap define extra space between all children.          Parameters:           rows                      (type=int)              cols                      (type=int)              vgap                      (type=int)              hgap                      (type=int)      #添加多个构件到boxsizer方法      AddMany(selfitems)      AddMany is a convenience method for adding several items to a sizer at one time. Simply pass it a list of tuples, where each tuple consists of the parameters that you would normally pass to the Add method.      #控件伸缩扩展方法(idx即指定的行号与列号..)      AddGrowableCol(self, idx, proportion=0)      Specifies that column idx (starting from zero) should be grown if there is extra space available to the sizer.      The proportion parameter has the same meaning as the stretch factor for the box sizers except that if all proportions are 0, then all columns are resized equally (instead of not being resized at all).          Parameters:           idx                      (type=size_t)              proportion                      (type=int)      AddGrowableRow(self, idx, proportion=0)      Specifies that row idx (starting from zero) should be grown if there is extra space available to the sizer.      The proportion parameter has the same meaning as the stretch factor for the box sizers except that if all proportions are 0, then all columns are resized equally (instead of not being resized at all).          Parameters:           idx                      (type=size_t)              proportion                      (type=int

实例2:

 
  1. def __init__(self, parent):  
  2.         wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)  
  3.           
  4.         wred = wx.TextCtrl(self, wx.NewId())  
  5.         wwhite = wx.TextCtrl(self, wx.NewId())  
  6.         wblue = wx.TextCtrl(self, wx.NewId())  
  7.         wcyan = wx.TextCtrl(self, wx.NewId())  
  8.         b1 = wx.Button(self, wx.NewId(), '&OK')  
  9.         b2 = wx.Button(self, wx.NewId(), '&Canel')  
  10.         st = wx.StaticText(self, -1, 'new flexgridsizer')  
  11.           
  12.         """hgap, vgap = 0, 0 
  13.         nrows, ncols = 2, 3 
  14.         fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap) 
  15.          
  16.         b = 5 
  17.         fgs.AddMany([(wred, 1, wx.EXPAND | wx.ALL, b), 
  18.                      (wwhite, 1, wx.EXPAND | wx.ALL, b), 
  19.                      (wblue, 1, wx.EXPAND | wx.ALL, b), 
  20.                      (wcyan, 1, wx.EXPAND | wx.ALL,b), 
  21.                      (b1, 0, wx.ALIGN_RIGHT), 
  22.                      (b2, 0, wx.ALIGN_LEFT | wx.LEFT, b), 
  23.                      ]) 
  24.         fgs.AddGrowableRow(0) 
  25.         fgs.AddGrowableRow(1) 
  26.         fgs.AddGrowableCol(0) 
  27.         fgs.AddGrowableCol(1) 
  28.         fgs.AddGrowableCol(2)"""  
  29.           
  30.         b = 0  
  31.         hsizer1 = wx.BoxSizer(wx.HORIZONTAL)  
  32.         hsizer1.Add(wred, 0, wx.ALL, b)  
  33.         hsizer1.Add((-1, -1), 1)  
  34.         hsizer1.Add(wwhite, 0, wx.ALL, b)  
  35.         hsizer1.Add((-1, -1), 1)  
  36.         hsizer1.Add(wblue, 0, wx.ALL, b)  
  37.           
  38.         vsizer1 = wx.BoxSizer(wx.VERTICAL)  
  39.         vsizer1.Add(wcyan, 0, wx.ALL, b)  
  40.         vsizer1.SetItemMinSize(wcyan, (100, 200)) # 设置widgets min size  
  41.         vsizer1.Add((-1, -1), 1)  
  42.         vsizer1.Add(b1, 0, wx.ALL, b)  
  43.         vsizer1.Add((-1, -1), 1)  
  44.         vsizer1.Add(b2, 0, wx.ALL, b)  
  45.           
  46.         hgap, vgap = 0, 0  
  47.         nrows, ncols = 2, 2  
  48.         fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)  
  49.           
  50.         b =5  
  51.         fgs.AddMany([(vsizer1, 1, wx.EXPAND | wx.ALL, b),  
  52.                      (st, 1, wx.EXPAND | wx.ALL, b),  
  53.                      ((-1, -1), 1, wx.EXPAND | wx.ALL, b),  
  54.                      (hsizer1, 1, wx.EXPAND | wx.ALL, b),  
  55.                      ])  
  56.         fgs.AddGrowableRow(0) # 第1行扩展  
  57.         fgs.AddGrowableCol(1) # 第2列扩展      
  58.           
  59.           
  60.         self.SetSizer(fgs)  

 Note: 其中使用到了wx.BoxSizer()与wx.FlexGridSizer()的结合.
 

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值