AUI(Advanced User Interface)模块使得我们可以方便地开发出美观、易用的用户界面。
面板管理
如下所示的界面包含了3个面板,面板实现了关闭、拖动、Dock、最大化等功能,这可以通过很少的代码方便地实现:
代码如下所示:
1: #!/usr/bin/env python 2: 3: import wx 4: import wx.aui 5: 6: class MyFrame(wx.Frame): 7: 8: def __init__(self, parent, id=-1, title='wx.aui Test', 9: pos=wx.DefaultPosition, size=(800, 600), 10: style=wx.DEFAULT_FRAME_STYLE): 11: wx.Frame.__init__(self, parent, id, title, pos, size, style) 12: 13: self._mgr = wx.aui.AuiManager(self) 14: 15: # create several text controls 16: text1 = wx.TextCtrl(self, -1, 'Pane 1 - sample text', 17: wx.DefaultPosition, wx.Size(200,150), 18: wx.NO_BORDER | wx.TE_MULTILINE) 19: 20: text2 = wx.TextCtrl(self, -1, 'Pane 2 - sample text', 21: wx.DefaultPosition, wx.Size(200,150), 22: wx.NO_BORDER | wx.TE_MULTILINE) 23: 24: text3 = wx.TextCtrl(self, -1, 'Main content window', 25: wx.DefaultPosition, wx.Size(200,150), 26: wx.NO_BORDER | wx.TE_MULTILINE) 27: 28: # add the panes to the manager 29: self._mgr.AddPane(window=text1, info=wx.aui.AuiPaneInfo().Left().MaximizeButton(True)) 30: self._mgr.AddPane(window=text2, info=wx.aui.AuiPaneInfo().Bottom().MaximizeButton(True)) 31: self._mgr.AddPane(window=text3, info=wx.aui.AuiPaneInfo().Center()) 32: 33: # tell the manager to 'commit' all the changes just made 34: self._mgr.Update() 35: 36: self.Bind(wx.EVT_CLOSE, self.OnClose) 37: 38: def OnClose(self, event): 39: # deinitialize the frame manager 40: self._mgr.UnInit() 41: # delete the frame 42: self.Destroy() 43: 44: app = wx.App() 45: frame = MyFrame(None) 46: frame.Show() 47: app.MainLoop()
如上代码所示,AUI简单易用,其使用方法总结如下:
以wx.Frame或其子类的实例为参数,用来生成一个AuiManager实例以实现对该Frame实例的管理;
self._mgr = wx.aui.AuiManager(self)
AuiManager中可以添加若干个面板;它使用AuiPaneInfo用来控制面板的位置、大小等属性;(参见行29-31)
调用Update()方法将AuiManager中的面板添加到其所管理的Frame中;
self._mgr.Update()
退出应用程序时应调用UnInit方法以回收资源。
self._mgr.UnInit()
工具栏
工具栏也可以作为面板添加在AuiManager中,实现工具栏的浮动等功能;如下图所示:
需要在面板管理中的代码中添加两段代码:生成和添加工具栏。
生成工具栏
1: # Create toolbar 2: toolbar = wx.ToolBar(self, -1, wx.DefaultPosition, wx.DefaultSize, 3: wx.TB_FLAT | wx.TB_NODIVIDER | wx.TB_HORZ_TEXT) 4: 5: toolbar.SetToolBitmapSize(wx.Size(16,16)) 6: toolbar_bmp1 = wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, wx.Size(16, 16)) 7: toolbar.AddLabelTool(101, "Item 1", toolbar_bmp1) 8: toolbar.AddLabelTool(101, "Item 2", toolbar_bmp1) 9: toolbar.AddLabelTool(101, "Item 3", toolbar_bmp1) 10: toolbar.AddLabelTool(101, "Item 4", toolbar_bmp1) 11: toolbar.AddSeparator() 12: toolbar.AddLabelTool(101, "Item 5", toolbar_bmp1) 13: toolbar.AddLabelTool(101, "Item 6", toolbar_bmp1) 14: toolbar.AddLabelTool(101, "Item 7", toolbar_bmp1) 15: toolbar.AddLabelTool(101, "Item 8", toolbar_bmp1) 16: toolbar.Realize()
添加工具栏
1: self._mgr.AddPane(toolbar, wx.aui.AuiPaneInfo(). 2: Name("toolbar").Caption("Toolbar Demo"). 3: ToolbarPane().Top(). 4: LeftDockable(False).RightDockable(False))
参考资源
http://www.wxpython.org/docs/api/wx.aui-module.html