如果wx.Toolbar對象的style參數設置為wx.TB_DOCKABLE,它成為可停靠。浮動工具欄還可以用wxPython中的AUIToolBar類來構造。
構造函數不帶任何參數則使用工具欄默認參數。附加參數可以傳遞給wx.ToolBar類構造如下 -
Wx.ToolBar(parent, id, pos, size, style)
| S.N. |
參數和說明
|
|---|---|
| 1 |
wx.TB_FLAT
提供該工具欄平面效果
|
| 2 |
wx.TB_HORIZONTAL
指定水準佈局(默認)
|
| 3 |
wxTB_VERTICAL
指定垂直佈局
|
| 4 |
wx.TB_DEFAULT_STYLE
結合wxTB_FLAT和wxTB_HORIZONTAL
|
| 5 |
wx.TB_DOCKABLE
使工具欄浮動和可停靠
|
| 6 |
wx.TB_NO_TOOLTIPS
當滑鼠懸停在工具欄不顯示簡短幫助工具提示,
|
| 7 |
wx.TB_NOICONS
指定工具欄按鈕沒有圖示;默認它們是顯示的
|
| 8 |
wx.TB_TEXT
顯示在工具欄按鈕上的文本;默認情況下,只有圖示顯示
|
| S.N. |
方法和說明
|
|---|---|
| 1 |
AddTool()
添加工具按鈕到工具欄。工具的類型是由各種參數指定的
|
| 2 |
AddRadioTool()
添加屬於按鈕的互斥組按鈕
|
| 3 |
AddCheckTool()
添加一個切換按鈕到工具欄
|
| 4 |
AddLabelTool()
使用圖示和標籤來添加工具欄
|
| 5 |
AddSeparator()
添加一個分隔符號來表示工具按鈕組
|
| 6 |
AddControl()
添加任何控制工具欄。 例如,wx.Button,wx.Combobox等。
|
| 7 |
ClearTools()
刪除所有在工具欄的按鈕
|
| 8 |
RemoveTool()
從給出工具按鈕移除工具欄
|
| 9 |
Realize()
工具按鈕增加調用
|
AddTool(parent, id, bitmap)
父參數是在按鈕被添加到工具欄。通過位圖bitmap參數所指定圖像圖示。
工具按鈕發出EVT_TOOL事件。如果添加到工具欄其他控制必須由各自CommandEvent綁定器到事件處理程式約束。
實例
tb = wx.ToolBar( self, -1 ) self.ToolBar = tb
tb.AddTool( 101, wx.Bitmap("new.png") )
tb.AddTool(102,wx.Bitmap("save.png"))
right = tb.AddRadioTool(222,wx.Bitmap("right.png"))
center = tb.AddRadioTool(333,wx.Bitmap("center.png"))
justify = tb.AddRadioTool(444,wx.Bitmap("justify.png"))
self.combo = wx.ComboBox(tb, 555, value = "Times", choices = ["Arial","Times","Courier"])
tb.Realize()
tb.Bind(wx.EVT_TOOL, self.Onright) tb.Bind(wx.EVT_COMBOBOX,self.OnCombo)
相應的事件處理程式以追加方式處理該事件源。雖然EVT_TOOL事件的ID會顯示在工具欄下方的文本框中,選中的字體名稱添加到它的時候,EVT_COMBOBOX事件觸發。
def Onright(self, event): self.text.AppendText(str(event.GetId())+"\n") def OnCombo(self,event): self.text.AppendText( self.combo.GetValue()+"\n")
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title)
self.InitUI()
def InitUI(self):
menubar = wx.MenuBar()
menu = wx.Menu()
menubar.Append(menu,"File")
self.SetMenuBar(menubar)
tb = wx.ToolBar( self, -1 )
self.ToolBar = tb
tb.AddTool( 101, wx.Bitmap("new.png") )
tb.AddTool(102,wx.Bitmap("save.png"))
right = tb.AddRadioTool(222,wx.Bitmap("right.png"))
center = tb.AddRadioTool(333,wx.Bitmap("center.png"))
justify = tb.AddRadioTool(444,wx.Bitmap("justify.png"))
tb.Bind(wx.EVT_TOOL, self.Onright)
tb.Bind(wx.EVT_COMBOBOX,self.OnCombo)
self.combo = wx.ComboBox( tb, 555, value = "Times", choices = ["Arial","Times","Courier"])
tb.AddControl(self.combo )
tb.Realize()
self.SetSize((350, 250))
self.text = wx.TextCtrl(self,-1, style = wx.EXPAND|wx.TE_MULTILINE)
self.Centre()
self.Show(True)
def Onright(self, event):
self.text.AppendText(str(event.GetId())+"\n")
def OnCombo(self,event):
self.text.AppendText( self.combo.GetValue()+"\n")
ex = wx.App()
Mywin(None,'ToolBar Demo - www.xuhuhu.com')
ex.MainLoop()

