]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ToolBar.py
5 #---------------------------------------------------------------------------
7 class TestToolBar(wx
.Frame
):
8 def __init__(self
, parent
, log
):
9 wx
.Frame
.__init
__(self
, parent
, -1, 'Test ToolBar', size
=(500, 300))
12 self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
)
14 wx
.Window(self
, -1).SetBackgroundColour(wx
.NamedColour("WHITE"))
16 # Use the wxFrame internals to create the toolbar and associate it all
17 # in one tidy method call.
18 tb
= self
.CreateToolBar( wx
.TB_HORIZONTAL
24 # Here's a 'simple' toolbar example, and how to bind it using SetToolBar()
25 #tb = wx.ToolBarSimple(self, -1, wx.DefaultPosition, wx.DefaultSize,
26 # wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT)
28 # But we're doing it a different way here.
30 log
.write("Default toolbar tool size: %s\n" % tb
.GetToolBitmapSize())
32 self
.CreateStatusBar()
34 tb
.AddSimpleTool(10, images
.getNewBitmap(), "New", "Long help for 'New'")
35 #tb.AddLabelTool(10, "New", images.getNewBitmap(), shortHelp="New", longHelp="Long help for 'New'")
36 self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=10)
37 self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=10)
39 tb
.AddSimpleTool(20, images
.getOpenBitmap(), "Open", "Long help for 'Open'")
40 self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=20)
41 self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=20)
44 tb
.AddSimpleTool(30, images
.getCopyBitmap(), "Copy", "Long help for 'Copy'")
45 self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=30)
46 self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=30)
48 tb
.AddSimpleTool(40, images
.getPasteBitmap(), "Paste", "Long help for 'Paste'")
49 self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=40)
50 self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=40)
54 tool
= tb
.AddCheckTool(50, images
.getTog1Bitmap(),
55 shortHelp
="Toggle this")
56 self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=50)
58 ## tb.AddCheckTool(60, images.getTog1Bitmap(), images.getTog2Bitmap(),
59 ## shortHelp="Toggle with 2 bitmaps")
60 ## self.Bind(EVT_TOOL, self.OnToolClick, id=60)
62 self
.Bind(wx
.EVT_TOOL_ENTER
, self
.OnToolEnter
)
63 self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
) # Match all
64 self
.Bind(wx
.EVT_TIMER
, self
.OnClearSB
)
71 tb
, cbID
, "", choices
=["", "This", "is a", "wxComboBox"],
72 size
=(150,-1), style
=wx
.CB_DROPDOWN
75 self
.Bind(wx
.EVT_COMBOBOX
, self
.OnCombo
, id=cbID
)
76 tb
.AddControl(wx
.TextCtrl(tb
, -1, "Toolbar controls!!", size
=(150, -1)))
78 # Final thing to do for a toolbar is call the Realize() method. This
79 # causes it to render (more or less, that is).
83 def OnToolClick(self
, event
):
84 self
.log
.WriteText("tool %s clicked\n" % event
.GetId())
85 tb
= self
.GetToolBar()
86 tb
.EnableTool(10, not tb
.GetToolEnabled(10))
88 def OnToolRClick(self
, event
):
89 self
.log
.WriteText("tool %s right-clicked\n" % event
.GetId())
91 def OnCombo(self
, event
):
92 self
.log
.WriteText("combobox item selected: %s\n" % event
.GetString())
94 def OnToolEnter(self
, event
):
95 self
.log
.WriteText('OnToolEnter: %s, %s\n' % (event
.GetId(), event
.GetInt()))
97 if self
.timer
is None:
98 self
.timer
= wx
.Timer(self
)
100 if self
.timer
.IsRunning():
103 self
.timer
.Start(2000)
107 def OnClearSB(self
, event
): # called for the timer event handler
108 self
.SetStatusText("")
113 def OnCloseWindow(self
, event
):
114 if self
.timer
is not None:
119 #---------------------------------------------------------------------------
121 class TestPanel(wx
.Panel
):
122 def __init__(self
, parent
, log
):
124 wx
.Panel
.__init
__(self
, parent
, -1)
126 b
= wx
.Button(self
, -1, "Show the ToolBar sample", (50,50))
127 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
130 def OnButton(self
, evt
):
131 win
= TestToolBar(self
, self
.log
)
135 #---------------------------------------------------------------------------
138 def runTest(frame
, nb
, log
):
139 win
= TestPanel(nb
, log
)
142 #---------------------------------------------------------------------------
148 wx.ToolBar is a narrow strip of icons on one side of a frame (top, bottom, sides)
149 that acts much like a menu does, except it is always visible. Additionally, actual
150 wxWindows controls, such as wx.TextCtrl or wx.ComboBox, can be added to the toolbar
151 and used from within it.
153 Toolbar creation is a two-step process. First, the toolbar is defined using the
154 various Add* methods of wx.ToolBar. Once all is set up, then wx.Toolbar.Realize()
155 must be called to render it.
157 wx.Toolbar events are also propogated as Menu events; this is especially handy when
158 you have a menu bar that contains items that carry out the same function. For example,
159 it is not uncommon to have a little 'floppy' toolbar icon to 'save' the current file
160 (whatever it is) as well as a FILE/SAVE menu item that does the same thing. In this
161 case, both events can be captured and acted upon using the same event handler
164 If there are cases where a toolbar icon should *not* be associated with a menu item,
165 use a unique ID to trap it.
167 There are a number of ways to create a toolbar for a wx.Frame. wx.Frame.CreateToolBar()
168 does all the work except it adds no buttons at all unless you override the virtual method
169 OnCreateToolBar(). On the other hand, you can just subclass wx.ToolBar and then use
170 wx.Frame.SetToolBar() instead.
172 Note that wx.TB_DOCKABLE is only supported under GTK. An attempt to alleviate this
173 is provided in wx.lib.floatbar, but it is not formally supported.
177 if __name__
== '__main__':
180 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])