📜  wxPython-布局管理

📅  最后修改于: 2020-11-05 04:58:41             🧑  作者: Mango


通过指定其以像素为单位测量的绝对坐标,可以将GUI小部件放置在容器窗口内。坐标相对于由其构造函数的size参数定义的窗口的尺寸。窗口小部件在窗口中的位置由其构造函数的pos参数定义。

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()

但是,由于以下原因,此绝对定位不适合-

  • 即使调整窗口大小,小部件的位置也不会改变。

  • 在具有不同分辨率的不同显示设备上,外观可能不一致。

  • 布局修改很困难,因为可能需要重新设计整个表单。

wxPython API提供了Layout类,以便更优雅地管理容器内小部件的位置。布局管理器相对于绝对定位的优势是-

  • 窗口内的小部件会自动调整大小。
  • 确保具有不同分辨率的显示设备上的外观均匀。
  • 无需重新设计,就可以动态添加或删除小部件。

布局管理器在wxPython中称为Sizer。 Wx.Sizer是所有sizer子类的基类。让我们讨论一些重要的大小调整器,例如wx.BoxSizer,wx.StaticBoxSizer,wx.GridSizer,wx.FlexGridSizer和wx.GridBagSizer。

S.N. Sizers & Description
1 BoxSizer

This 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 GridSizer

As 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 FlexiGridSizer

This sizer also has a two dimensional grid. However, it provides little more flexibility in laying out the controls in the cells.

4 GridBagSizer

GridBagSizer is a versatile sizer. It offers more enhancements than FlexiGridSizer. Child widget can be added to a specific cell within the grid.

5 StaticBoxSizer

A StaticBoxSizer puts a box sizer into a static box. It provides a border around the box along with a label at the top.