下面看一下關於創建工具欄,狀態欄和菜單的方法,看下面一個例子:
import wx
class ToolBarFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'ToolBar',size=(300,200))
panel=wx.Panel(self)
panel.SetBackgroundColour('white')
statusBar=self.CreateStatusBar()
toolBar=self.CreateToolBar()
toolBar.AddSimpleTool(wx.NewId(),wx.Bitmap('Toolbar.bmp'),"New","long help for 'New'")
toolBar.Realize()
menuBar=wx.MenuBar()
menu1=wx.Menu()
menuBar.Append(menu1,"&File")
menu2=wx.Menu()
menu2.Append(wx.NewId(),"&Copy","Copy in status bar")
menu2.Append(wx.NewId(),"&Cut","")
menu2.Append(wx.NewId(),"Paste","")
menu2.AppendSeparator()
menu2.Append(wx.NewId(),"&Options...","Display Option")
menuBar.Append(menu2,"&Edit")
self.SetMenuBar(menuBar)
if __name__=='__main__':
app=wx.PySimpleApp()
frame=ToolBarFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()
運行結果如下:
首先是StatusBar的創建:statusBar=self.CreateStatusBar() 這里用到了Frame里的一個方法,CreateStatusBar(),它就默認在當前的frame下面創建一個默認的和frame邊緣相符的狀態欄,這是非常簡單的,一句話搞定。當然wx為我們提供了一個專門的ToolBar類,和其對應的很多方法,比如Create(),這里就先不介紹了。StatusBar就是顯示一些別的應用提供的文本,這里文本的大小等屬性由系統默認。
下面是ToolBar的創建。
toolBar=self.CreateToolBar()
toolBar.AddSimpleTool(wx.NewId(),wx.Bitmap('Toolbar.bmp'),"New","long help for 'New'")
toolBar.Realize()
第一句還是調用了Frame里的一個方法CreateToolBar,返回一個ToolBar對象,也是非常的簡單一種方法,下面是往這個ToolBar上加載我們要的圖標,AddSimpleTool的用法可以通過help來幫助:AddSimpleTool(self, id, bitmap, shortHelpString='', longHelpString='', isToggle=0) unbound wx._controls.ToolBar method
Old style method to add a tool to the toolbar.
其中的一個參數longHelpString就是要顯示到狀態欄的幫助信息。最后Realize()就是要讓這個工具欄顯示在窗口上。
最后剩下的就是創建菜單了。
MenuBar()是創建菜單欄,也就是菜單要放置的地方。Menu()是創建菜單,Append()是把菜單加到菜單欄的方法,或者是把子菜單加到菜單上。self.SetMenuBar(menuBar)是調用了Frame的一個方法,來放置菜單欄,它會自動放置到合適的位置。