Wxpython 简明教程
wxPython - Layout Management
可以通过指定 GUI 组件在容器窗口中的绝对坐标(以像素为单位)把它放在容器窗口里面。该坐标与窗口的尺寸相关,由窗口的构造函数的 size 参数定义。组件在窗口中的位置由其构造函数的 pos 参数定义。
A GUI widget can be placed inside the container window by specifying its absolute coordinates measured in pixels. The coordinates are relative to the dimensions of the window defined by size argument of its constructor. Position of the widget inside the window is defined by pos argument of its constructor.
import wx
app = wx.App()
window = wx.Frame(None, title = "wxPython Frame", size = (300,200))
panel = wx.Panel(window)
label = wx.StaticText(panel, label = "Hello World", pos = (100,50))
window.Show(True)
app.MainLoop()
然而,这个 Absolute Positioning 由于以下原因不可行 −
This Absolute Positioning however is not suitable because of the following reasons −
-
The position of the widget does not change even if the window is resized.
-
The appearance may not be uniform on different display devices with different resolutions.
-
Modification in the layout is difficult as it may need redesigning the entire form.
wxPython API 提供了 Layout 类来更优雅地管理容器中组件的位置。布局管理器在绝对定位方面有以下优点 −
wxPython API provides Layout classes for more elegant management of positioning of widgets inside the container. The advantages of Layout managers over absolute positioning are −
-
Widgets inside the window are automatically resized.
-
Ensures uniform appearance on display devices with different resolutions.
-
Adding or removing widgets dynamically is possible without having to redesign.
布局管理器在 wxPython 中被称为 Sizer。Wx.Sizer 是所有 sizer 子类的基类。让我们讨论一些重要的 sizer,例如 wx.BoxSizer、wx.StaticBoxSizer、wx.GridSizer、wx.FlexGridSizer 和 wx.GridBagSizer。
Layout manager is called Sizer in wxPython. Wx.Sizer is the base class for all sizer subclasses. Let us discuss some of the important sizers such as wx.BoxSizer, wx.StaticBoxSizer, wx.GridSizer, wx.FlexGridSizer, and wx.GridBagSizer.
S.N. |
Sizers & Description |
1 |
BoxSizerThis sizer allows the controls to be arranged in row-wise or column-wise manner. BoxSizer’s layout is determined by its orientation argument (either wxVERTICAL or wxHORIZONTAL). |
2 |
GridSizerAs the name suggests, a GridSizer object presents a two dimensional grid. Controls are added in the grid slot in the left-to-right and top-to-bottom order. |
3 |
FlexiGridSizerThis sizer also has a two dimensional grid. However, it provides little more flexibility in laying out the controls in the cells. |
4 |
GridBagSizerGridBagSizer is a versatile sizer. It offers more enhancements than FlexiGridSizer. Child widget can be added to a specific cell within the grid. |
5 |
StaticBoxSizerA StaticBoxSizer puts a box sizer into a static box. It provides a border around the box along with a label at the top. |