我无法理解这一点:在 sizer.Add 中,设置了比例以标识子小部件的缩放比例,而 wx.EXPAND 告诉子小部件展开以占据可用宽度。但是当没有将标志设置为 wx.EXPAND 并将比例设置为 1 时,子小部件仍然会扩展。
那么这两件事之间的关系/区别是什么?
谢谢
该比例特定于 sizer 类型(awx.GridSizer
将创建大小相等的 X*Y 单元格,因此不使用该比例)。
使用 a wx.BoxSizer
,比例指定元素沿 sizer 轴的比例。将元素添加到 awx.BoxSizer(wx.HORIZONTAL)
将在水平轴上proportion > 0
缩放项目。带有 的元素将缩放到带有 的元素的两倍大小。比例为 0 表示不应缩放元素。proportion=2
proportion=1
wx.EXPAND
指定元素应该增长以填充沿另一个轴的可用空间。将元素添加到wx.BoxSizer(wx.HORIZONTAL)
withwx.EXPAND
将扩展垂直轴上的项目。
这个demo会演示效果:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='BoxSizer demo (resize the frame)')
self.SetBackgroundColour(wx.WHITE)
layout = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.BoxSizer(wx.HORIZONTAL)"),wx.HORIZONTAL)
# proportion=1
layout.Add( wx.StaticText(self,label='proportion=1', style=wx.BORDER_DOUBLE ), 1, wx.ALL, 10 )
# proportion=0, expand
layout.Add( wx.StaticText(self,label='wx.EXPAND', style=wx.BORDER_DOUBLE ), 0, wx.EXPAND|wx.ALL, 10 )
# proportion=0, align bottom
layout.Add( wx.StaticText(self,label='wx.ALIGN_BOTTOM', style=wx.BORDER_DOUBLE ), 0, wx.ALIGN_BOTTOM|wx.ALL, 10 )
# proportion=2, expand
layout.Add( wx.StaticText(self,label='proportion=2 + wx.EXPAND', style=wx.BORDER_DOUBLE ), 2, wx.EXPAND|wx.ALL, 10 )
self.SetSizerAndFit(layout)
app = wx.App(False)
Frame().Show()
app.MainLoop()
下一个演示演示了垂直框尺寸器中的水平框尺寸器:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='BoxSizer demo (resize the frame)')
self.SetBackgroundColour(wx.WHITE)
layout = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.VERTICAL"),wx.VERTICAL)
# row 1
row = wx.wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=0"),wx.HORIZONTAL)
row.Add(wx.StaticText(self,label="proportion=0",style=wx.BORDER_DOUBLE),0, wx.ALL,10)
row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
layout.Add(row) # add row 1 without arguments
# row 2
row = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=1"),wx.HORIZONTAL)
row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
row.Add(wx.StaticText(self,label="proportion=2, wx.EXPAND",style=wx.BORDER_DOUBLE),2, wx.EXPAND|wx.ALL,10)
layout.Add(row,1) # add row 2 with proportion = 1
# row 3
row = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=0, wx.EXPAND"),wx.HORIZONTAL)
row.Add(wx.StaticText(self,label="proportion=0",style=wx.BORDER_DOUBLE),0, wx.ALL,10)
row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
layout.Add(row,0,wx.EXPAND) # add row 3 with proportion = 0 and wx.EXPAND
self.SetSizerAndFit(layout)
app = wx.App(False)
Frame().Show()
app.MainLoop()