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

xiaoxiao2021-03-01  25

对于初学者,wxPython的布局定位是有点麻烦,需要仔细查看官方文档。(原文: wiki.wxpython.org/learnSizer3.py)         以下是我学习过程中实现的实例,以便与大家共同交流:)     1. wx.BoxSizer() 在wxPython定位构件程中使用最多最直观的sizer.      原型:   wx.BoxSizer.__init__(self, orient)    sizer = wx.BoxSizer(wx.HORIZONTAL)   #创建一个水平方向的box ; wx.VERTICAL ,垂直方向的box      它还有以下常用到的方法:   Add(self, item, proportion, flag, border, userData)  Insert(self, before, item, proportion, flag, border, userData, realIndex)  Layout(self)  Prepend(self, item, proportion, flag, border, userData)  Remove(self, indx, pop)  Show(self, item, show)  ...(原文: http://www.wxpython.org/docs/api/wx.BoxSizer-class.html)    实例1:   def __init__(self, parent):          wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)                    b = 5          vsizer1 = wx.BoxSizer(orient=wx.VERTICAL)          wbtn1 = wx.Button(self, -1, 'Botton1')          wbtn2 = wx.Button(self, -1, 'Botton2')            vsizer1.Add(item=wbtn1, proportion=1, flag=wx.EXPAND|wx.ALL, border=b)          vsizer1.Add(wbtn2, 0, wx.ALIGN_CENTER | wx.ALL, b)        # wx.ALIGN_LEFT, wx.ALIGN_RIGHT          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:   def __init__(self, parent):          wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.DefaultSize)                    wred = wx.TextCtrl(self, wx.NewId())          wwhite = wx.TextCtrl(self, wx.NewId())          wblue = wx.TextCtrl(self, wx.NewId())          wcyan = wx.TextCtrl(self, wx.NewId())          b1 = wx.Button(self, wx.NewId(), '&OK')          b2 = wx.Button(self, wx.NewId(), '&Canel')          st = wx.StaticText(self, -1, 'new flexgridsizer')                    """hgap, vgap = 0, 0         nrows, ncols = 2, 3         fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)                  b = 5         fgs.AddMany([(wred, 1, wx.EXPAND | wx.ALL, b),                      (wwhite, 1, wx.EXPAND | wx.ALL, b),                      (wblue, 1, wx.EXPAND | wx.ALL, b),                      (wcyan, 1, wx.EXPAND | wx.ALL,b),                      (b1, 0, wx.ALIGN_RIGHT),                      (b2, 0, wx.ALIGN_LEFT | wx.LEFT, b),                      ])         fgs.AddGrowableRow(0)         fgs.AddGrowableRow(1)         fgs.AddGrowableCol(0)         fgs.AddGrowableCol(1)         fgs.AddGrowableCol(2)"""                    b = 0          hsizer1 = wx.BoxSizer(wx.HORIZONTAL)          hsizer1.Add(wred, 0, wx.ALL, b)          hsizer1.Add((-1, -1), 1)          hsizer1.Add(wwhite, 0, wx.ALL, b)          hsizer1.Add((-1, -1), 1)          hsizer1.Add(wblue, 0, wx.ALL, b)                    vsizer1 = wx.BoxSizer(wx.VERTICAL)          vsizer1.Add(wcyan, 0, wx.ALL, b)          vsizer1.SetItemMinSize(wcyan, (100, 200)) # 设置widgets min size          vsizer1.Add((-1, -1), 1)          vsizer1.Add(b1, 0, wx.ALL, b)          vsizer1.Add((-1, -1), 1)          vsizer1.Add(b2, 0, wx.ALL, b)                    hgap, vgap = 0, 0          nrows, ncols = 2, 2          fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)                    b =5          fgs.AddMany([(vsizer1, 1, wx.EXPAND | wx.ALL, b),                       (st, 1, wx.EXPAND | wx.ALL, b),                       ((-1, -1), 1, wx.EXPAND | wx.ALL, b),                       (hsizer1, 1, wx.EXPAND | wx.ALL, b),                       ])          fgs.AddGrowableRow(0) # 第1行扩展          fgs.AddGrowableCol(1) # 第2列扩展                                  self.SetSizer(fgs)    Note: 其中使用到了wx.BoxSizer()与wx.FlexGridSizer()的结合.      
转载请注明原文地址: https://www.6miu.com/read-4149986.html

最新回复(0)