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 class TestPanel( wx.Panel ) :
  62     """
  63     A Panel class with one or two attached toolbars.
  64     """
  65 
  66     def __init__( self, parent, id=wx.ID_ANY, size=wx.DefaultSize, color="BLUE", numToolbars=1 ) :
  67 
  68         wx.Panel.__init__( self, parent, id, wx.Point( 0, 0 ), size, wx.SUNKEN_BORDER )
  69         self.WindowColor = color
  70 
  71         ## Create the vertical sizer for the toolbar and Panel
  72         box = wx.BoxSizer( wx.VERTICAL )
  73         tb = self.BuildToolbar1()
  74         box.Add( tb, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND, 4 ) # add the toolbar to the sizer
  75         
  76         if numToolbars == 2 : # Do we want the second toolbar?
  77             tb = self.BuildToolbar2()
  78             box.Add( tb, 0, wx.ALL | wx.ALIGN_RIGHT , 4 )# This one gets aligned to the right
  79 
  80         #Now add a Window to draw stuff to ( this could be any wx.Window derived control ) 
  81         self.DrawWindow = wx.Window( self, -1, wx.DefaultPosition, wx.DefaultSize, wx.SUNKEN_BORDER )
  82         box.Add( self.DrawWindow, 1, wx.EXPAND )
  83 
  84         box.Fit( self )
  85         self.SetAutoLayout( True )
  86         self.SetSizer( box )
  87 
  88         # this connects the OnPaint handler to when the DrawWindow needs to be re-painted
  89         wx.EVT_PAINT( self.DrawWindow, self.OnPaint )
  90     
  91     #end __init__
  92     
  93     #-----
  94     
  95     def BuildToolbar1( self ) :
  96 
  97         """
  98         creates one of the toolbars
  99 
 100         The buttons act like radio buttons, setting a mode for the Panel
 101         Only one of them is pressed at a time. The SetMode() method handles this
 102 
 103         """
 104         
 105         tb = wx.ToolBar( self, -1 )
 106         self.ToolBar = tb
 107         tb.SetToolBitmapSize( ( 21, 21 ) )# this required for non-standard size buttons on MSW
 108         
 109         tb.AddTool( ID_ZOOM_IN_BUTTON, eid.GetPlusBitmap(), isToggle=True )
 110         wx.EVT_TOOL( self, ID_ZOOM_IN_BUTTON, self.SetMode )
 111         
 112         tb.AddTool( ID_ZOOM_OUT_BUTTON, eid.GetMinusBitmap(), isToggle=True )
 113         wx.EVT_TOOL( self, ID_ZOOM_OUT_BUTTON, self.SetMode )
 114         
 115         tb.AddTool( ID_MOVE_MODE_BUTTON, eid.GetHandBitmap(), isToggle=True )
 116         wx.EVT_TOOL( self, ID_MOVE_MODE_BUTTON, self.SetMode )
 117         
 118         tb.AddSeparator()   # Invisible spacer
 119         
 120         # A way to insert a custom separator.
 121         tb.AddControl( wx.StaticBitmap( tb, wx.ID_ANY, eid.GetSeparatorBitmap() ) )
 122         
 123         tb.AddSeparator()   # Invisible spacer
 124         
 125         tb.AddControl( wx.Button( tb, ID_TEST_BUTTON, "Button", wx.DefaultPosition, wx.DefaultSize ) )
 126         wx.EVT_BUTTON( self, ID_TEST_BUTTON, self.ButtonAction )
 127         
 128         tb.Realize()
 129         
 130         return tb
 131         
 132     #end BuildToolbar1 def
 133     
 134     #-----
 135     
 136     def BuildToolbar2( self ) :
 137 
 138         """
 139         Creates another toolbar. It looks the same, but acts a little different :
 140         The buttons are independent, rather than acting like radio buttons
 141 
 142         It also has a custom separator, created by adding a tall skinny bitmap.
 143         """
 144         
 145         tb = wx.ToolBar( self, id=wx.ID_ANY )
 146         self.ToolBar2 = tb
 147         tb.SetToolBitmapSize( (21, 21) ) # Square spacer equired for non-standard size buttons on MSW
 148         
 149         tb.AddTool( ID_ZOOM_IN_BUTTON2, eid.GetPlusBitmap(), isToggle=True )
 150         wx.EVT_TOOL( self, ID_ZOOM_IN_BUTTON2, self.ButtonPress2 )
 151         
 152         tb.AddTool( ID_ZOOM_OUT_BUTTON2, eid.GetMinusBitmap(), isToggle=True )
 153         wx.EVT_TOOL( self, ID_ZOOM_OUT_BUTTON2, self.ButtonPress2 )
 154         
 155         tb.AddTool( ID_MOVE_MODE_BUTTON2, eid.GetHandBitmap(), isToggle=True )
 156         wx.EVT_TOOL( self, ID_MOVE_MODE_BUTTON2, self.ButtonPress2 )
 157         
 158         tb.AddSeparator()   # Invisible spacer
 159         
 160         # A way to insert a custom separator.
 161         tb.AddControl( wx.StaticBitmap( tb, wx.ID_ANY, eid.GetSeparatorBitmap() ) )
 162         
 163         tb.AddSeparator()   # Invisible spacer
 164         
 165         tb.AddControl( wx.Button( tb, ID_TEST_BUTTON2, "Button", wx.DefaultPosition, wx.DefaultSize ) )
 166         wx.EVT_BUTTON( self, ID_TEST_BUTTON2, self.ButtonPress2 )
 167         
 168         tb.Realize()
 169         
 170         return tb
 171         
 172     #end BuildToolbar2 def
 173     
 174     #-----
 175     
 176     def ButtonPress2( self, event ) :
 177         print "%s Panel second toolbar button clicked" % self.WindowColor
 178 
 179     def SetMode( self, event ) :
 180         
 181         # Deselect all the buttons.
 182         for id in [ID_ZOOM_IN_BUTTON, ID_ZOOM_OUT_BUTTON, ID_MOVE_MODE_BUTTON] :
 183             self.ToolBar.ToggleTool( id, 0 )
 184         
 185         # Select this button.
 186         self.ToolBar.ToggleTool( event.GetId(), 1 )
 187         if event.GetId() == ID_ZOOM_IN_BUTTON :
 188             print "Mode set to Zoom In in the %s Canvas" % self.WindowColor
 189         
 190         elif event.GetId() == ID_ZOOM_OUT_BUTTON :
 191             print "Mode set to Zoom Out in the %s Canvas" % self.WindowColor
 192         
 193         elif event.GetId() == ID_MOVE_MODE_BUTTON :
 194             print "Mode set to Move in the %s Canvas" % self.WindowColor
 195 
 196     def ButtonAction( self, event ) :
 197         print "%s Canvas button clicked" % self.WindowColor
 198         pass
 199 
 200     def OnPaint( self, event ) :
 201         dc = wx.PaintDC( self.DrawWindow )
 202         dc.SetBackground( wx.Brush( wx.NamedColour( self.WindowColor ) ) )
 203         dc.BeginDrawing()
 204         dc.Clear()
 205         dc.EndDrawing()
 206 
 207 #end TestPanel class
 208 
 209 #------------------------------------------------------------------------------
 210 
 211 class TestFrame( wx.Frame ) :
 212     
 213     def __init__( self, parent=None, id=wx.ID_ANY, title='Default Title', 
 214                   pos=wx.DefaultPosition, size=( 600, 500 )  ) :
 215         
 216         wx.Frame.__init__( self, parent, id, title, pos, size )
 217         
 218         wx.EVT_CLOSE( self, self.OnCloseWindow )
 219         
 220         #-----
 221         
 222         Canvas1 = TestPanel( self, color = "RED" )
 223         Canvas2 = TestPanel( self, color = "BLUE", numToolbars = 2 )
 224         
 225         #Build the Toolbar
 226         tb = self.CreateToolBar( wx.TB_HORIZONTAL|wx.NO_BORDER )
 227         self.ToolBar = tb
 228         tb.SetToolBitmapSize( ( 21, 21 ) )# this required for non-standard size buttons on MSW
 229 
 230         tb.AddTool( ID_ZOOM_IN_BUTTON, eid.GetPlusBitmap(), isToggle=True )
 231         wx.EVT_TOOL( self, ID_ZOOM_IN_BUTTON, self.SetMode )
 232 
 233         tb.AddTool( ID_ZOOM_OUT_BUTTON, eid.GetMinusBitmap(), isToggle=True )
 234         wx.EVT_TOOL( self, ID_ZOOM_OUT_BUTTON, self.SetMode )
 235 
 236         tb.AddTool( ID_MOVE_MODE_BUTTON, eid.GetHandBitmap(), isToggle=True )
 237         wx.EVT_TOOL( self, ID_MOVE_MODE_BUTTON, self.SetMode )
 238 
 239         tb.AddSeparator()   # Invisible spacer
 240         
 241         tb.AddControl( wx.Button( tb, ID_TEST_BUTTON, "Button", wx.DefaultPosition, wx.DefaultSize ) )
 242         wx.EVT_BUTTON( self, ID_TEST_BUTTON, self.ButtonAction )
 243 
 244         tb.AddSeparator()   # Invisible spacer
 245         
 246         # A way to insert a custom separator.
 247         tb.AddControl( wx.StaticBitmap( tb, wx.ID_ANY, eid.GetSeparatorBitmap() ) )
 248         
 249         tb.AddSeparator()   # Invisible spacer
 250         
 251         msg = 'A Frame-Managed Toolbar - Try Resizing the Frame Horizontally !'
 252         tb.AddControl( wx.StaticText( tb, -1, msg ) )
 253 
 254         tb.Realize()
 255         
 256         ## Create the horizontal sizer for the two panels
 257         box = wx.BoxSizer( wx.HORIZONTAL )
 258 
 259         box.Add( Canvas1 , 1, wx.EXPAND )
 260         box.Add( Canvas2 , 2, wx.EXPAND )
 261 
 262         box.Fit( self )
 263         #self.SetAutoLayout()
 264         self.SetSizer( box )
 265         
 266         self.Show()
 267         
 268     #end __init__
 269 
 270     def SetMode( self, event ) :
 271         
 272         # Deselect all the buttons.
 273         for id in [ID_ZOOM_IN_BUTTON, ID_ZOOM_OUT_BUTTON, ID_MOVE_MODE_BUTTON] :
 274             self.ToolBar.ToggleTool( id, 0 )
 275         
 276         # Select this button.
 277         self.ToolBar.ToggleTool( event.GetId(), 1 )
 278         if event.GetId() == ID_ZOOM_IN_BUTTON :
 279             print "Mode set to Zoom In in the Frame"
 280         
 281         elif event.GetId() == ID_ZOOM_OUT_BUTTON :
 282             print "Mode set to Zoom Out in the Frame"
 283         
 284         elif event.GetId() == ID_MOVE_MODE_BUTTON :
 285             print "Mode set to Move in the Frame"
 286 
 287     def ButtonAction( self, event ) :
 288         print "Button clicked in the Frame"
 289         pass
 290 
 291     def OnCloseWindow( self, event ) :
 292         self.Destroy()
 293 
 294 #end TestFrame class
 295 
 296 #==============================================================================
 297 
 298 if __name__ == "__main__" :
 299     
 300     app = wx.App( redirect=False )
 301     
 302     appFrame = TestFrame(  None, wx.ID_ANY, "Embedded Graphics Toolbar Test", 
 303                           pos=( 100, 100 ), size=( 550, 200 ) )
 304     #app.SetTopWindow( appFrame )
 305     
 306     app.MainLoop()
 307 
 308 #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.
  • [get | view] (2012-05-24 23:49:21, 33.4 KB) [[attachment:EditorPanelWithToolbar_Demo.png]]
  • [get | view] (2012-05-24 23:49:09, 26.8 KB) [[attachment:EditorPanelWithToolbar_Demo.py]]
  • [get | view] (2012-05-25 00:03:35, 32.0 KB) [[attachment:EditorPanelWithToolbar_IN_ACTION.png]]
  • [get | view] (2011-05-25 00:12:34, 2.5 KB) [[attachment:EmbeddedIconData.py]]
  • [get | view] (2011-05-25 00:14:13, 17.2 KB) [[attachment:wxToolBar_Embedded_DEMO.png]]
  • [get | view] (2011-05-25 00:14:25, 10.7 KB) [[attachment:wxToolBar_Embedded_DEMO.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.