Attachment 'HatchedWindow.py'

Download

   1 #!/usr/bin/python
   2 
   3 """
   4 Demo of a window having a hatched background pattern.
   5 """
   6 
   7 import sys
   8 import os
   9 
  10 import wx
  11 
  12 #---------------------------------------------------------------------------
  13 ############################################################################
  14 #---------------------------------------------------------------------------
  15 
  16 class ParentContainerControl( wx.Frame ) :
  17    
  18     def __init__( self, parent ) :
  19         
  20         titleTxt = 'HatchedWindow'
  21         wx.Frame.__init__( self, parent, -1, title=titleTxt )
  22         self.SetClientSize( (400, 350) )
  23         self.Bind( wx.EVT_SIZE, self.OnSize )
  24         
  25         self.frmPanel = wx.Panel( self, -1)
  26         self.frmPanel.SetBackgroundColour( (240, 200, 175) )    # pink
  27         self.frmPanel.Bind( wx.EVT_LEFT_DCLICK, self.OnClose )
  28         
  29         #-----
  30         
  31         self.CreateStatusBar()
  32         statusTxt = 'HatchedWindow managed with position and size'
  33         self.SetStatusText( statusTxt )
  34         
  35         #-----
  36         
  37         # Every wx.EVT_SIZE event will position and size self.hatchedWin.
  38         # Leaving __init__ will cause the first ex.EVT_SIZE event,
  39         #   so, there's no need to do anything here in __init__
  40         #   in additiona to just creating the hatched window object.
  41         self.hatchedWinBorder = 15      # arbitrary value
  42         self.hatchedWin = HatchedWindow( self, self.hatchedWinBorder )
  43         
  44     #end __init__
  45     
  46     #----------------------------------
  47     
  48     def OnSize( self, event ) :
  49         """
  50         Set the position and size of the hatched window.
  51         This is specifically in lieu of using a sizer to manage the hatched window.
  52         A wx.EVT_SIZE event in this parent object generates a wx.EVT_PAINT event 
  53         in all child objects created in this class (meaning, instantiated from the 
  54         class definition).
  55         """
  56         
  57         # Manage this classes own panel.
  58         self.frmPanel.SetSize( self.GetClientSize() )
  59         
  60         #-----
  61         
  62         # Set the position and size of the hatched window.
  63         self.CenterControlInClient( self.hatchedWin, self.hatchedWinBorder)
  64         
  65         self.Refresh()
  66         
  67     #end def
  68     
  69     #----------------------------------
  70     
  71     def CenterControlInClient( self, control, border ) :
  72         
  73         control.SetPosition( (border, border) )
  74         
  75         selfSizeX, selfSizeY = self.GetClientSize()
  76         controlSizeX = selfSizeX - 2*border
  77         controlSizeY = selfSizeY - 2*border
  78         control.SetSize( (controlSizeX, controlSizeY) )
  79         
  80     #end def
  81     
  82     #----------------------------------
  83     
  84     def OnClose( self, event ) :
  85         
  86         self.Destroy()      # Exit app.MainLoop()
  87         
  88     #end def
  89 
  90 #end ParentContainerControl class
  91 
  92 #---------------------------------------------------------------------------
  93 ############################################################################
  94 #---------------------------------------------------------------------------
  95 
  96 class HatchedWindow( wx.Window ) :
  97     
  98     def __init__( self, parent, border=30 ) :
  99         
 100         self.border = border
 101         
 102         wx.Window.__init__( self, parent, -1 )
 103         self.SetBackgroundColour( 'WHITE' )             # use any color
 104         self.Bind( wx.EVT_PAINT, self.OnPaint )
 105         
 106     #end __init__
 107     
 108     #----------------------------------
 109     
 110     def OnPaint( self, event=None ) :
 111         
 112         dc = wx.PaintDC( self )
 113         selfSizeX, selfSizeY = self.GetSize()
 114         
 115         # Define the background pattern. The HatchedWindow background color is retained.
 116         hatchColor = (0, 75, 0)                 # dark green - use any color
 117         dc.SetPen( wx.Pen( hatchColor, 1 ) )
 118         bgPattern = wx.BDIAGONAL_HATCH          # See the various wx.Brush() styles.
 119         dc.SetBrush( wx.Brush( hatchColor, bgPattern ) )
 120         
 121         dc.DrawRectangle( 0, 0, selfSizeX, selfSizeY )
 122         
 123         """ 
 124         # Overdraw the original rectangle outline with another color.
 125         dc.SetPen( wx.Pen( 'wHiTe', 5 ) )                   # use any color
 126         dc.SetBrush( wx.Brush( 'BluE', wx.TRANSPARENT ) )   # Leave the interior intact
 127         dc.DrawRectangle( 0, 0, selfSizeX, selfSizeY )
 128         """ 
 129         
 130         event.Skip()
 131         
 132     #end OnPaint def
 133         
 134 #end PanelHatchedBG class
 135 
 136 #==============================================================================
 137 
 138 if __name__ == '__main__'  :
 139     
 140     app = wx.App( redirect=False )
 141     
 142     appFrame = ParentContainerControl( None )
 143     appFrame.Show()
 144     
 145     app.MainLoop()
 146         
 147 #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] (2011-07-12 16:33:20, 18.7 KB) [[attachment:HatchedWinSizedUsingSLider_1.png]]
  • [get | view] (2011-07-12 16:34:10, 22.0 KB) [[attachment:HatchedWinSizedUsingSLider_2.png]]
  • [get | view] (2011-07-12 16:34:19, 20.1 KB) [[attachment:HatchedWinSizedUsingSLider_3.png]]
  • [get | view] (2011-07-12 16:34:33, 8.8 KB) [[attachment:HatchedWinSizedUsingSLider_4.png]]
  • [get | view] (2011-07-14 04:03:38, 10.2 KB) [[attachment:HatchedWin_Slider.py]]
  • [get | view] (2011-07-12 16:42:17, 4.7 KB) [[attachment:HatchedWindow.py]]
  • [get | view] (2011-07-12 16:40:42, 19.0 KB) [[attachment:HatchedWindow_1.png]]
  • [get | view] (2011-07-12 16:40:49, 14.9 KB) [[attachment:HatchedWindow_2.png]]
 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.