Attachment 'HatchedWin_Slider.py'
Download 1 #!/usr/bin/python
2
3 import sys
4
5 import wx
6
7 DEBUG = True
8 DEBUG = False # Comment this line to see console info statements.
9
10 #------------------------------------------------------------------------------
11 ###############################################################################
12 #------------------------------------------------------------------------------
13
14 class ParentControl( wx.Frame ) :
15
16 def __init__( self ) :
17
18 # Frame setup
19 titleTxt = 'HatchedWinSizedUsingSLider'
20 wx.Frame.__init__( self, None, -1, title=titleTxt, pos=(10, 20))
21 self.SetClientSize( (400, 300) )
22 self.Bind( wx.EVT_SIZE, self.OnSize )
23
24 frmPanel = wx.Panel( self, -1)
25 frmPanel.SetBackgroundColour( (200, 230, 240) )
26 frmPanel.Bind( wx.EVT_LEFT_DCLICK, self.OnClose )
27
28 self.CreateStatusBar()
29 self.SetStatusText( titleTxt )
30
31 #----- Frame Contents
32
33 # Create a container in which to place the hatched window.
34 self.hwPanel = wx.Panel( frmPanel )
35 self.hwPanel.SetBackgroundColour( (250, 240, 240) )
36 self.hwPanel.Bind( wx.EVT_LEFT_DCLICK, self.OnClose )
37
38 # Initial value, min value = 0, max value = 100
39 # The BG color is the color of frmPanel
40 self.horzSlider = wx.Slider( frmPanel, -1,
41 80, 0, 100,
42 (0, 0), # Using a sizer: position is ignored
43 (self.GetClientSize()[0]-15, 50), # size
44 wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS )
45
46 # Respond to changes in slider position.
47 # This will cause the hatched window size to be changed.
48 self.Bind( wx.EVT_SLIDER, self.OnSlider )
49 self.sliderValue = self.horzSlider.GetValue()
50 self.prevSliderValue = self.sliderValue
51
52 #-----
53
54 # Auto-manage size and placement of self.hwPanel and self.horzSlider
55 self.frmPnl_vSzr = wx.BoxSizer( wx.VERTICAL )
56
57 # This panel takes up all the frmPanel client area
58 # not claimed by the slider control.
59 self.frmPnl_vSzr.Add( self.hwPanel, 1, wx.EXPAND ) # Expand along both major and minor axes
60 self.frmPnl_vSzr.Add( self.horzSlider, 0, wx.EXPAND|wx.ALL, 5 ) # Keep fixed vert size
61 frmPanel.SetSizer( self.frmPnl_vSzr )
62 self.Layout()
63
64 # Get the hatched window to initially show.
65 # This also enables
66 self.frmPnl_vSzr.Fit( frmPanel)
67
68 # The hatched window's size and position will be calculated
69 # and set by CenterHatchedWinInHwPanel() defined below.
70 # Auto-centering without resizing can't be accomplished by using sizers.
71 # Both EVT_SIZE and EVT_SLIDER handlers call CenterHatchedWinInHwPanel().
72 self.hatchedWin = HatchedWindow( self.hwPanel )
73
74 #----- For de-bug info
75
76 self.OnSizeCtr = 0 # For de-bug
77 self.OnPaintCtr = 0 # For de-bug
78
79 #end __init__
80
81 #----------------------------------
82
83 def RedrawImageWindow( self ) :
84 """
85 A hack that is very simple and works very well.
86 """
87
88 selfSizeX, selfSizeY = self.GetSize()
89
90 # Marginally bump up, then back down the Frame size.
91 self.SetSize( (selfSizeX+1, selfSizeY+1) )
92 self.SetSize( (selfSizeX, selfSizeY) )
93
94 #end def
95
96 #----------------------------------
97
98 def OnSize( self, event ) :
99 """
100 Define/redefine the position and size of the hatched window
101 A wx.EVT_SIZE event generates a wx.EVT_PAINT event in objects
102 # created from the class (instantiated from the class definition).
103
104 Every wx.EVT_SIZE event generates a wx.EVT_PAINT event.
105 This paint handler is just a pass-through.
106 """
107
108 # For de-bug
109 self.OnSizeCtr += 1
110 if DEBUG :
111 print '-'*50
112 print '---- ParentControl::OnSize() self.OnSizeCtr ', self.OnSizeCtr
113 print ' self.GetClientSize() ', self.GetClientSize()
114 print ' self.hatchedWin.GetSize() ', self.hatchedWin.GetSize()
115 #end if
116
117 self.CenterHatchedWinInHwPanel()
118
119 event.Skip() # Necessary or even desireable ?
120
121 #end def
122
123 #----------------------------------
124
125 def CenterHatchedWinInHwPanel( self ) :
126
127 # The slider value [0..100] will set the hatched window size to [0..100%]
128 # of each client axis containing self.hwPanel
129 hwPanelWidth, hwPanelHeight = self.hwPanel.GetClientSize()
130
131 # Resize hatchedWin
132 newHwinWidth = int( (self.sliderValue / 100.0) * hwPanelWidth + 0.5)
133 newHwinHeight = int( (self.sliderValue / 100.0) * hwPanelHeight + 0.5)
134 newWinSize = (newHwinWidth, newHwinHeight)
135 self.hatchedWin.SetSize( newWinSize )
136
137 # Reposition hatchedWin
138 newHwinPosX = (hwPanelWidth - newHwinWidth) / 2
139 newHwinPosY = (hwPanelHeight - newHwinHeight) / 2
140 newHwinPos = (newHwinPosX, newHwinPosY)
141 self.hatchedWin.SetPosition ( newHwinPos )
142
143 self.Layout() # The sizer will resize and reposition its controls.
144
145 self.hwPanel.Refresh( False )
146
147 #end def
148
149 #----------------------------------
150
151 def OnPaint( self, event=None ) :
152 """ STUB """
153
154 if DEBUG :
155 self.OnPaintCtr += 1
156 print 'STUB ParentControl::OnPaint()', self.OnPaintCtr; print
157
158 event.Skip()
159
160 #end OnPaint def
161
162 #----------------------------------
163
164 def OnSlider( self, event ):
165 """
166 The slider value [0..100] will redefine the hatched window size
167 to [0..100%] of the dimensions of the self.hwPanel client dimensions.
168 """
169 if DEBUG :
170 print '---- OnSlider():'
171
172 self.sliderValue = self.horzSlider.GetValue()
173 if (self.sliderValue != self.prevSliderValue) :
174
175 statStr = 'Slider @ %3d' % (self.sliderValue)
176 self.SetStatusText( statStr )
177
178 if DEBUG :
179 print ' Current, Previous Slider Value', \
180 self.sliderValue, self.prevSliderValue
181
182 self.SendSizeEvent()
183
184 self.prevSliderValue = self.sliderValue
185
186 self.RedrawImageWindow()
187
188 #end if
189
190 #end OnSlider def
191
192 #----------------------------------
193
194 def OnClose( self, event ) :
195
196 if DEBUG :
197 print '---- ParentControl::OnClose()'
198
199 self.Destroy() # Exit app.MainLoop()
200
201 #end def
202
203 #end ParentControl class
204
205 #------------------------------------------------------------------------------
206 ###############################################################################
207 #------------------------------------------------------------------------------
208
209 class HatchedWindow( wx.Window ) :
210
211 def __init__( self, parent ) :
212
213 wx.Window.__init__( self, parent, -1 )
214 self.Bind( wx.EVT_LEFT_DCLICK, self.OnClose )
215 self.SetBackgroundColour( 'WHitE' )
216 self.Bind( wx.EVT_PAINT, self.OnPaint )
217 self.Bind( wx.EVT_SIZE, self.OnSize )
218
219 #-----
220
221 self.OnPaintCtr = 0 # For de-bug console messages
222 self.OnSizeCtr = 0 # For de-bug
223
224 #end __init__
225
226 #----------------------------------
227
228 def OnSize( self, event=None ) :
229 """ STUB """
230
231 if DEBUG :
232 self.OnSizeCtr += 1
233 print 'STUB HatchedWindow::OnSize()', self.OnSizeCtr
234 print
235
236 event.Skip()
237
238 #end def
239
240 #----------------------------------
241
242 def OnPaint( self, event=None ) :
243
244 if DEBUG :
245 self.OnPaintCtr += 1
246 print '---- HatchedWindow::OnPaint()', self.OnPaintCtr
247 print '-'*50
248
249 dc = wx.PaintDC( self )
250
251 # Define the background pattern.
252 hatchColor = (0, 75, 0) # dark green
253 dc.SetPen( wx.Pen( hatchColor, 1 ) ) # See wx.Brush() styles
254 dc.SetBrush( wx.Brush( hatchColor, wx.BDIAGONAL_HATCH ) )
255 selfSizeX, selfSizeY = self.GetSize()
256 dc.DrawRectangle( 0, 0, selfSizeX, selfSizeY )
257
258 event.Skip()
259
260 #end OnPaint def
261
262 #----------------------------------
263
264 def OnClose( self, event ) :
265
266 if DEBUG :
267 print 'STUB ParentControl::OnClose()'
268 wx.Exit() # Exit app.MainLoop()
269
270 #end def
271
272 #end PanelHatchedBG class
273
274 #------------------------------------------------------------------------------
275
276 def WhatsInstalled() :
277
278 # What packages are installed ?
279 import os, sys, platform
280 print
281 if os.name == 'nt' :
282 print 'Windows ', platform.win32_ver()[1]
283 else :
284 print 'Platform ', platform.system()
285 #end if
286 print 'Python ', sys.version
287 addon_pkgs = [ ('Wx Version', 'wx.VERSION_STRING'),
288 ('Wx Pltform', 'wx.PlatformInfo'), ]
289 try :
290 import Image # Doesn't need to be installed.
291 pilStr = 'PIL '
292 pilAddonStr = 'Image.VERSION'
293 addon_pkgs.append( (pilStr , pilAddonStr) )
294 except :
295 pass
296 #end try
297 for addonStr, attribute in addon_pkgs :
298 try :
299 print addonStr, eval( attribute )
300 except NameError :
301 print
302 #end try
303 #end for
304 print
305
306 #end WhatsInstalled def
307
308 #==============================================================================
309
310 if __name__ == '__main__' :
311
312 WhatsInstalled()
313
314 app = wx.App( redirect=False )
315
316 appFrame = ParentControl()
317 appFrame.Show()
318
319 app.MainLoop()
320
321 #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.