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

