Wxpython 简明教程
wxPython - Multiple Document Interface
典型的 GUI 应用程序可能有多个窗口。标签式和小部件可随时激活一个这样的窗口。但是,很多时候此方法可能没有用,因为其他窗口的视图被隐藏了。
A typical GUI application may have multiple windows. Tabbed and stacked widgets allow to activate one such window at a time. However, many a times this approach may not be useful as view of other windows is hidden.
同时显示多个窗口的一种方式是将它们创建为独立的窗口。这称为 SDI ( Single Document Interface )。这样做需要更多内存资源,因为每个窗口可能都有其自己的菜单系统、工具栏等。
One way to display multiple windows simultaneously is to create them as independent windows. This is called as SDI (Single Document Interface). This requires more memory resources as each window may have its own menu system, toolbar, etc.
wxPython 中的 MDI 框架提供了一个 wx.MDIParentFrame 类。其对象用作多个子窗口的容器,每个都是 wx.MDIChildFrame 类的对象。
MDI framework in wxPython provides a wx.MDIParentFrame class. Its object acts as a container for multiple child windows, each an object of wx.MDIChildFrame class.
子窗口位于父框架的 MDIClientWindow 区域中。一旦添加一个子框架,父框架的菜单栏就会显示一个窗口菜单,其中包含按钮以级联或平铺方式排列子窗口。
Child windows reside in the MDIClientWindow area of the parent frame. As soon as a child frame is added, the menu bar of the parent frame shows a Window menu containing buttons to arrange the children in a cascaded or tiled manner.
Example
以下示例说明了 MDIParentFrame 作为顶级窗口的用法。名为 NewWindow 的菜单按钮会在客户端区域添加一个子窗口。可以添加多个窗口,然后以级联或平铺顺序排列。
The following example illustrates the uses of MDIParentFrame as top level window. A Menu button called NewWindow adds a child window in the client area. Multiple windows can be added and then arranged in a cascaded or tiled order.
完整代码如下所示:
The complete code is as follows −
import wx
class MDIFrame(wx.MDIParentFrame):
def __init__(self):
wx.MDIParentFrame.__init__(self, None, -1, "MDI Parent", size = (600,400))
menu = wx.Menu()
menu.Append(5000, "&New Window")
menu.Append(5001, "&Exit")
menubar = wx.MenuBar()
menubar.Append(menu, "&File")
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.OnNewWindow, id = 5000)
self.Bind(wx.EVT_MENU, self.OnExit, id = 5001)
def OnExit(self, evt):
self.Close(True)
def OnNewWindow(self, evt):
win = wx.MDIChildFrame(self, -1, "Child Window")
win.Show(True)
app = wx.App()
frame = MDIFrame()
frame.Show()
app.MainLoop()
上述代码生成以下输出 -
The above code produces the following output −