Attachment 'wxToolBar_Embedded_Demo.py'
Download 1 #!/usr/bin/env python2.3
2
3 """
4 wxToolBar_DEMO.py - Toolbar generation using embedded images.
5 WorkingWithToolBars @ http ://wiki.wxpython.org/WorkingWithToolBars
6 Chris.Barker@noaa.gov 200?
7
8 Updated and modified by Ray Pasco 2011-04-24
9 pascor@verion.net
10
11
12 # Note: I use python2.3 above because my system has 2.2, 2.1, and 1.5.2 installed also
13
14 This is demo of a number of things that can be done with toolbars.
15 When run from a console, it writes messages to indicate toolbar button events.
16
17 This includes :
18 - Having it managed by a Frame : the most basic way of creating a toolbar
19
20 - Placing a toolbar in a wxPanel with a sizer so that you can have a
21 custom panel with a toolbar attached, rather than a frame
22
23 - Placing multiple toolbars in a Panel. This could be done inside a frame, instead.
24
25 - Using a wxStaticBitmap on a toolbar : Use custom separator (one that's visible)
26 Do this if you don't like the one that comes with the stock widget,
27 or want the same look on all platforms.
28
29 - Making a set of buttons on a toolbar act like radio buttons : That is,
30 only one is shown to be pressed at a time.
31
32 A few things in the demo that are not strictly about toolbars :
33
34 - Multiple panels, each with its own toolbar.
35
36 - Uses code generated by the old version of img2py.py to embed icons into Python code.
37
38 """
39
40 import wx
41
42 # It's easiest to generate & manage embedded graphics data when it's in a single separate file.
43 import EmbeddedIconData as eid
44
45 #------------------------------------------------------------------------------
46
47 ID_ZOOM_IN_BUTTON = wx.NewId()
48 ID_ZOOM_OUT_BUTTON = wx.NewId()
49 ID_TEST_BUTTON = wx.NewId()
50 ID_MOVE_MODE_BUTTON = wx.NewId()
51
52 ID_ZOOM_IN_BUTTON2 = wx.NewId()
53 ID_ZOOM_OUT_BUTTON2 = wx.NewId()
54 ID_TEST_BUTTON2 = wx.NewId()
55 ID_MOVE_MODE_BUTTON2 = wx.NewId()
56
57 ID_TOOLBAR = wx.NewId()
58
59 #------------------------------------------------------------------------------
60 #==============================================================================
61 #------------------------------------------------------------------------------
62
63 class TestFrame( wx.Frame ) :
64
65 def __init__( self, parent=None, id=wx.ID_ANY, title='Default Title',
66 pos=wx.DefaultPosition, size=(700, 500) ) :
67
68 wx.Frame.__init__( self, parent, id, title, pos, size )
69 wx.EVT_CLOSE( self, self.OnCloseWindow )
70
71 #-----
72
73 Canvas1 = TestPanel( self, color="RED" )
74 Canvas2 = TestPanel( self, color="BLUE", numToolbars=2 )
75
76 tb = wx.ToolBar( self, -1 )
77 self.ToolBar = tb
78 tb.SetToolBitmapSize( (21, 21) ) # Required on MSW for non-standard size buttons.
79
80 tb.AddTool( ID_ZOOM_IN_BUTTON, eid.GetPlusBitmap(), isToggle=True )
81 wx.EVT_TOOL( self, ID_ZOOM_IN_BUTTON, self.SetMode )
82
83 tb.AddTool( ID_ZOOM_OUT_BUTTON, eid.GetMinusBitmap(), isToggle=True )
84 wx.EVT_TOOL( self, ID_ZOOM_OUT_BUTTON, self.SetMode )
85
86 tb.AddTool( ID_MOVE_MODE_BUTTON, eid.GetHandBitmap(), isToggle=True )
87 wx.EVT_TOOL( self, ID_MOVE_MODE_BUTTON, self.SetMode )
88
89 tb.AddSeparator() # Invisible spacer
90
91 tb.AddControl( wx.Button( tb, ID_TEST_BUTTON, "Button", wx.DefaultPosition, wx.DefaultSize ) )
92 wx.EVT_BUTTON( self, ID_TEST_BUTTON, self.ButtonAction )
93
94 tb.AddSeparator() # Invisible spacer
95
96 # A way to insert a custom separator.
97 tb.AddControl( wx.StaticBitmap( tb, wx.ID_ANY, eid.GetSeparatorBitmap() ) )
98
99 tb.AddSeparator() # Invisible spacer
100
101 msg = 'A Frame-Managed Toolbar - Try Resizing the Frame Horizontally !'
102 tb.AddControl( wx.StaticText( tb, -1, msg ) )
103
104 tb.Realize()
105
106 ## Create the horizontal sizer for the two panels
107 box = wx.BoxSizer( wx.HORIZONTAL )
108
109 box.Add( Canvas1 , 1, wx.EXPAND )
110 box.Add( Canvas2 , 2, wx.EXPAND )
111
112 #box.Fit( self )
113 self.SetSizer( box )
114
115 self.Show()
116
117 #end __init__
118
119 #--------------------------------------------------------------------------
120
121 def SetMode( self, event ) :
122
123 # Deselect all the buttons.
124 for id in [ID_ZOOM_IN_BUTTON, ID_ZOOM_OUT_BUTTON, ID_MOVE_MODE_BUTTON] :
125 self.ToolBar.ToggleTool( id, 0 )
126
127 # Select this button.
128 self.ToolBar.ToggleTool( event.GetId(), 1 )
129 if event.GetId() == ID_ZOOM_IN_BUTTON :
130 print "Mode set to Zoom In in the Frame"
131
132 elif event.GetId() == ID_ZOOM_OUT_BUTTON :
133 print "Mode set to Zoom Out in the Frame"
134
135 elif event.GetId() == ID_MOVE_MODE_BUTTON :
136 print "Mode set to Move in the Frame"
137
138 #--------------------------------------------------------------------------
139
140 def ButtonAction( self, event ) :
141 print "Button clicked in the Frame"
142 pass
143
144 #--------------------------------------------------------------------------
145
146 def OnCloseWindow( self, event ) :
147 self.Destroy()
148
149 #end TestFrame class
150
151 #------------------------------------------------------------------------------
152 #==============================================================================
153 #------------------------------------------------------------------------------
154
155 class TestPanel( wx.Panel ) :
156 """
157 A Panel class with one or two attached toolbars.
158 """
159
160 def __init__( self, parent, id=wx.ID_ANY, size=wx.DefaultSize, color="BLUE", numToolbars=1 ) :
161
162 wx.Panel.__init__( self, parent, id, wx.Point( 0, 0 ), size, wx.SUNKEN_BORDER )
163 self.WindowColor = color
164
165 ## Create the vertical sizer for the toolbar and Panel
166 box = wx.BoxSizer( wx.VERTICAL )
167 tb = self.BuildToolbar1()
168 box.Add( tb, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND, 4 ) # add the toolbar to the sizer
169
170 if numToolbars == 2 : # Is a second toolbar wanted ?
171 tb = self.BuildToolbar2()
172 box.Add( tb, 0, wx.ALL | wx.ALIGN_RIGHT , 4 )# This one gets aligned to the right
173
174 #Now add a Window to draw stuff to ( this could be any wx.Window derived control )
175 self.DrawWindow = wx.Window( self, -1, wx.DefaultPosition, wx.DefaultSize, wx.SUNKEN_BORDER )
176 box.Add( self.DrawWindow, 1, wx.EXPAND )
177
178 #box.Fit( self )
179 self.SetAutoLayout( True )
180 self.SetSizer( box )
181
182 # this connects the OnPaint handler to when the DrawWindow needs to be re-painted
183 wx.EVT_PAINT( self.DrawWindow, self.OnPaint )
184
185 #end __init__
186
187 #-----
188
189 def BuildToolbar1( self ) :
190
191 """
192 creates one of the toolbars
193
194 The buttons act like radio buttons, setting a mode for the Panel
195 Only one of them is pressed at a time. The SetMode() method handles this
196
197 """
198
199 tb = wx.ToolBar( self, -1 )
200 self.ToolBar = tb
201 # this required for non-standard size buttons on MSW
202 tb.SetToolBitmapSize( eid.GetPlusBitmap().GetSize() )
203
204 tb.AddTool( ID_ZOOM_IN_BUTTON, eid.GetPlusBitmap(), isToggle=True )
205 wx.EVT_TOOL( self, ID_ZOOM_IN_BUTTON, self.SetMode )
206
207 tb.AddTool( ID_ZOOM_OUT_BUTTON, eid.GetMinusBitmap(), isToggle=True )
208 wx.EVT_TOOL( self, ID_ZOOM_OUT_BUTTON, self.SetMode )
209
210 tb.AddTool( ID_MOVE_MODE_BUTTON, eid.GetHandBitmap(), isToggle=True )
211 wx.EVT_TOOL( self, ID_MOVE_MODE_BUTTON, self.SetMode )
212
213 tb.AddSeparator() # Invisible spacer
214
215 # A way to insert a custom separator.
216 tb.AddControl( wx.StaticBitmap( tb, wx.ID_ANY, eid.GetSeparatorBitmap() ) )
217
218 tb.AddSeparator() # Invisible spacer
219
220 tb.AddControl( wx.Button( tb, ID_TEST_BUTTON, "Button", wx.DefaultPosition, wx.DefaultSize ) )
221 wx.EVT_BUTTON( self, ID_TEST_BUTTON, self.ButtonAction )
222
223 tb.Realize()
224
225 return tb
226
227 #end BuildToolbar1 def
228
229 #-----
230
231 def BuildToolbar2( self ) :
232
233 """
234 Creates another toolbar. It looks the same, but acts a little different :
235 The buttons are independent, rather than acting like radio buttons
236
237 It also has a custom separator, created by adding a tall skinny bitmap.
238 """
239
240 tb = wx.ToolBar( self, id=-1 )
241 self.ToolBar2 = tb
242 # Square spacer equired for non-standard size buttons on MSW
243 tb.SetToolBitmapSize( eid.GetPlusBitmap().GetSize() )
244
245 tb.AddTool( ID_ZOOM_IN_BUTTON2, eid.GetPlusBitmap(), isToggle=True )
246 wx.EVT_TOOL( self, ID_ZOOM_IN_BUTTON2, self.ButtonPress2 )
247
248 tb.AddTool( ID_ZOOM_OUT_BUTTON2, eid.GetMinusBitmap(), isToggle=True )
249 wx.EVT_TOOL( self, ID_ZOOM_OUT_BUTTON2, self.ButtonPress2 )
250
251 tb.AddTool( ID_MOVE_MODE_BUTTON2, eid.GetHandBitmap(), isToggle=True )
252 wx.EVT_TOOL( self, ID_MOVE_MODE_BUTTON2, self.ButtonPress2 )
253
254 tb.AddSeparator() # Invisible spacer
255
256 # A way to insert a custom separator.
257 tb.AddControl( wx.StaticBitmap( tb, wx.ID_ANY, eid.GetSeparatorBitmap() ) )
258
259 tb.AddSeparator() # Invisible spacer
260
261 tb.AddControl( wx.Button( tb, ID_TEST_BUTTON2, "Button", wx.DefaultPosition, wx.DefaultSize ) )
262 wx.EVT_BUTTON( self, ID_TEST_BUTTON2, self.ButtonPress2 )
263
264 tb.Realize()
265
266 return tb
267
268 #end BuildToolbar2 def
269
270 #-----
271
272 def ButtonPress2( self, event ) :
273 print "%s Panel second toolbar button clicked" % self.WindowColor
274
275 def SetMode( self, event ) :
276
277 # Deselect all the buttons.
278 for id in [ID_ZOOM_IN_BUTTON, ID_ZOOM_OUT_BUTTON, ID_MOVE_MODE_BUTTON] :
279 self.ToolBar.ToggleTool( id, 0 )
280
281 # Select this button.
282 self.ToolBar.ToggleTool( event.GetId(), 1 )
283 if event.GetId() == ID_ZOOM_IN_BUTTON :
284 print "Mode set to Zoom In in the %s Canvas" % self.WindowColor
285
286 elif event.GetId() == ID_ZOOM_OUT_BUTTON :
287 print "Mode set to Zoom Out in the %s Canvas" % self.WindowColor
288
289 elif event.GetId() == ID_MOVE_MODE_BUTTON :
290 print "Mode set to Move in the %s Canvas" % self.WindowColor
291
292 def ButtonAction( self, event ) :
293 print "%s Canvas button clicked" % self.WindowColor
294 pass
295
296 def OnPaint( self, event ) :
297 dc = wx.PaintDC( self.DrawWindow )
298 dc.SetBackground( wx.Brush( wx.NamedColour( self.WindowColor ) ) )
299 dc.BeginDrawing()
300 dc.Clear()
301 dc.EndDrawing()
302
303 #end TestPanel class
304
305 #==============================================================================
306
307 if __name__ == "__main__" :
308
309 app = wx.App( redirect=False )
310
311 appFrame = TestFrame( None, wx.ID_ANY, "Embedded Graphics Toolbar Test",
312 pos=( 100, 100 ), size=( 550, 200 ) )
313 #app.SetTopWindow( appFrame )
314
315 app.MainLoop()
316
317 #end if
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.