Attachment 'PenAndBrushStyles.py'
Download 1 """
2 PenAndBrushStyles.py
3
4 Andrea Gavana
5 2011-08-09
6
7 https://groups.google.com/forum/#!topic/wxpython-users/ECgRONRxsmg
8 """
9
10 import os
11
12 import wx
13
14 from wx.lib.embeddedimage import PyEmbeddedImage
15
16 #------------------------------------------------------------------------------
17
18 class PenAndBrushFrame( wx.Frame ) :
19
20 def __init__( self ) :
21
22 wx.Frame.__init__( self, None, title="wx.Pen and wx.Brush Styles", size=(700, 600) )
23
24 # Convert a wx.Bitmap to a wx.Icon and then display it as a Frame titlebar graphic.
25 iconWxImage = mondrian.GetBitmap().ConvertToImage().Rescale( 16, 16 )
26 # There's no direct way to convert a wx.Bitmap or wx.Image to an wx.Icon.
27 iconTmpFilename = './Mondrian.ico'
28 iconWxImage.SaveFile( iconTmpFilename, wx.BITMAP_TYPE_ICO )
29 icon = wx.Icon( iconTmpFilename, wx.BITMAP_TYPE_ICO )
30 os.remove( iconTmpFilename )
31 self.SetIcon( icon )
32
33 #-----
34
35 # Set a Panel into the Frame to standardize the look across all platform types.
36 # Every visible control gets put on frmPanel.
37 frmPanel = wx.Panel( self )
38 frmPanel.SetBackgroundColour( (240, 240, 240) ) # eggshell white or "off-white"
39
40 font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT )
41 font.SetWeight( wx.BOLD )
42
43 frame_vSzr = wx.BoxSizer( wx.VERTICAL )
44
45 penLabel = 'Pen Styles (os.name = %s) :' % (os.name)
46 penLabel = wx.StaticText( frmPanel, -1, penLabel )
47 penLabel.SetFont( font )
48
49 frame_vSzr.Add( penLabel, 0, wx.EXPAND|wx.ALL, 10 )
50
51 grdSzr_top = wx.GridSizer( 4, 4, 3, 3 ) # rows, cols, vgap, hgap
52
53 for pen_name in pen_styles :
54 small = PenPanel( frmPanel, pen_name )
55 grdSzr_top.Add( small, 0, wx.EXPAND )
56 #end for
57
58 frame_vSzr.Add( grdSzr_top, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, 10 )
59
60 brushLabel = wx.StaticText( frmPanel, -1, "Brush Styles :" )
61 brushLabel.SetFont( font )
62
63 frame_vSzr.Add( brushLabel, 0, wx.EXPAND|wx.ALL, 10 )
64
65 grdSzr_bot = wx.GridSizer( 3, 3, 3, 3 ) # rows, cols, vgap, hgap
66
67 for aBrushStyle in brush_styles :
68 aBrushPanel = BrushPanel( frmPanel, aBrushStyle )
69 grdSzr_bot.Add( aBrushPanel, 0, wx.EXPAND )
70 #end for
71
72 frame_vSzr.Add( grdSzr_bot, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, 10 )
73
74 frmPanel.SetSizer( frame_vSzr )
75 self.Layout()
76
77 #end __init__
78
79 #end PenAndBrushFrame class
80
81 #------------------------------------------------------------------------------
82
83 pen_styles = [ "wx.SOLID", "wx.DOT", "wx.LONG_DASH",
84 "wx.SHORT_DASH", "wx.DOT_DASH", "wx.BDIAGONAL_HATCH",
85 "wx.CROSSDIAG_HATCH", "wx.FDIAGONAL_HATCH", "wx.CROSS_HATCH",
86 "wx.VERTICAL_HATCH", "wx.STIPPLE", "wx.USER_DASH" ]
87
88 brush_styles = [ "wx.SOLID", "wx.STIPPLE", "wx.BDIAGONAL_HATCH",
89 "wx.CROSSDIAG_HATCH", "wx.FDIAGONAL_HATCH", "wx.CROSS_HATCH",
90 "wx.HORIZONTAL_HATCH", "wx.VERTICAL_HATCH", "wx.TRANSPARENT" ]
91
92
93 mondrian = PyEmbeddedImage(
94 "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAHFJ"
95 "REFUWIXt1jsKgDAQRdF7xY25cpcWC60kioI6Fm/ahHBCMh+BRmGMnAgEWnvPpzK8dvrFCCCA"
96 "coD8og4c5Lr6WB3Q3l1TBwLYPuF3YS1gn1HphgEEEABcKERrGy0E3B0HFJg7C1N/f/kTBBBA"
97 "+Vi+AMkgFEvBPD17AAAAAElFTkSuQmCC" )
98
99 #------------------------------------------------------------------------------
100
101 class BasePanel( wx.Panel ) :
102
103 def __init__( self, parent ) :
104
105 wx.Panel.__init__( self, parent, style=wx.SUNKEN_BORDER|wx.WANTS_CHARS )
106
107 self.SetBackgroundStyle( wx.BG_STYLE_CUSTOM )
108
109 self.Bind( wx.EVT_SIZE, self.OnSize )
110 self.Bind( wx.EVT_PAINT, self.OnPaint )
111
112
113 def OnSize( self, event ) :
114
115 event.Skip()
116 self.Refresh()
117
118 #end class
119
120 #------------------------------------------------------------------------------
121
122 class PenPanel( BasePanel ) :
123
124 def __init__( self, parent, pen_name ) :
125
126 BasePanel.__init__( self, parent )
127 self.pen_name = pen_name
128
129 #end __init__
130
131 #----------------------------------
132
133 def OnPaint( self, event ) :
134
135 width, height = self.GetClientSize()
136
137 dc = wx.AutoBufferedPaintDC( self )
138 dc.SetBackground( wx.WHITE_BRUSH )
139 dc.Clear()
140
141 font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT )
142 dc.SetFont( font )
143
144 name = self.pen_name
145
146 if "STIPPLE" in name :
147 patternImg = mondrian.GetBitmap().ConvertToImage()
148 patternBmap = patternImg.Rescale( 10, 10 ).ConvertToBitmap()
149 pen = wx.Pen( wx.BLUE, patternBmap.GetWidth(), eval( self.pen_name ) )
150 pen.SetStipple( patternBmap )
151 else :
152 pen = wx.Pen( wx.BLUE, 2, eval( name ) )
153
154 if "USER" in name :
155 pen.SetDashes( [2, 5] )
156 name += " ( [2, 5] )"
157
158 dc.SetTextForeground( wx.BLACK )
159 dc.DrawText( name, 5, 5 )
160
161 dc.SetPen( pen )
162 dc.DrawLine( 5, 25, width-5, 25 ) # horizontal
163 dc.DrawLine( 5, 30, width-5, self.Size[1]-10 ) # diagonal U-L to L-R
164
165 #end PenPanel class
166
167 #------------------------------------------------------------------------------
168
169 class BrushPanel( BasePanel ) :
170
171 def __init__( self, parent, aBrushStyle ) :
172
173 BasePanel.__init__( self, parent )
174
175 self.aBrushStyle = aBrushStyle
176
177 #----------------------------------
178
179 def OnPaint( self, event ) :
180
181 width, height = self.GetClientSize()
182
183 dc = wx.AutoBufferedPaintDC( self )
184 dc.SetBackground( wx.WHITE_BRUSH )
185 dc.Clear()
186
187 font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT )
188 dc.SetFont( font )
189
190 dc.SetPen( wx.TRANSPARENT_PEN )
191 name = self.aBrushStyle
192
193 if "STIPPLE" in name :
194 patternImg = mondrian.GetBitmap().ConvertToImage()
195 patternImg.Rescale( 10, 10 )
196 patternImg.SetMask( False )
197 patternBmap = patternImg.ConvertToBitmap()
198 brush = wx.BrushFromBitmap( patternBmap )
199 else :
200 brush = wx.Brush( wx.BLUE, eval( name ) )
201
202 dc.SetTextForeground( wx.BLACK )
203 dc.DrawText( name, 5, 5 )
204
205 if "wx.TRANSPARENT" in name :
206 dc.SetPen( wx.Pen( wx.BLUE, 2, wx.SOLID ) )
207 dc.SetBrush( brush )
208 dc.DrawRectangle( 5, 30, width-10, height-35 )
209
210 #end OnPaint def
211
212 #end BrushPanel class
213
214 #==============================================================================
215
216 if __name__ == '__main__' :
217
218 app = wx.App( redirect=False )
219
220 appFrame = PenAndBrushFrame()
221 appFrame.CenterOnScreen()
222 appFrame.Show()
223
224 app.MainLoop()
225
226 #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.