]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/MaskedNumCtrl.py
7 import wx
.lib
.maskededit
as me
8 import wx
.lib
.maskednumctrl
as mnum
9 #----------------------------------------------------------------------
11 class TestPanel( wx
.Panel
):
12 def __init__( self
, parent
, log
):
14 wx
.Panel
.__init
__( self
, parent
, -1 )
16 panel
= wx
.Panel( self
, -1 )
18 header
= wx
.StaticText(panel
, -1, """\
19 This shows the various options for MaskedNumCtrl.
20 The controls at the top reconfigure the resulting control at the bottom.
22 header
.SetForegroundColour( "Blue" )
24 intlabel
= wx
.StaticText( panel
, -1, "Integer width:" )
25 self
.integerwidth
= mnum
.MaskedNumCtrl(
26 panel
, value
=10, integerWidth
=2, allowNegative
=False
29 fraclabel
= wx
.StaticText( panel
, -1, "Fraction width:" )
30 self
.fractionwidth
= mnum
.MaskedNumCtrl(
31 panel
, value
=0, integerWidth
=2, allowNegative
=False
34 groupcharlabel
= wx
.StaticText( panel
,-1, "Grouping char:" )
35 self
.groupchar
= me
.MaskedTextCtrl(
36 panel
, -1, value
=',', mask
='&', excludeChars
= '-()',
37 formatcodes
='F', emptyInvalid
=True, validRequired
=True
40 decimalcharlabel
= wx
.StaticText( panel
,-1, "Decimal char:" )
41 self
.decimalchar
= me
.MaskedTextCtrl(
42 panel
, -1, value
='.', mask
='&', excludeChars
= '-()',
43 formatcodes
='F', emptyInvalid
=True, validRequired
=True
46 self
.set_min
= wx
.CheckBox( panel
, -1, "Set minimum value:" )
47 # Create this MaskedNumCtrl using factory, to show how:
48 self
.min = mnum
.MaskedNumCtrl( panel
, integerWidth
=5, fractionWidth
=2 )
49 self
.min.Enable( False )
51 self
.set_max
= wx
.CheckBox( panel
, -1, "Set maximum value:" )
52 self
.max = mnum
.MaskedNumCtrl( panel
, integerWidth
=5, fractionWidth
=2 )
53 self
.max.Enable( False )
56 self
.limit_target
= wx
.CheckBox( panel
, -1, "Limit control" )
57 self
.allow_none
= wx
.CheckBox( panel
, -1, "Allow empty control" )
58 self
.group_digits
= wx
.CheckBox( panel
, -1, "Group digits" )
59 self
.group_digits
.SetValue( True )
60 self
.allow_negative
= wx
.CheckBox( panel
, -1, "Allow negative values" )
61 self
.allow_negative
.SetValue( True )
62 self
.use_parens
= wx
.CheckBox( panel
, -1, "Use parentheses" )
63 self
.select_on_entry
= wx
.CheckBox( panel
, -1, "Select on entry" )
64 self
.select_on_entry
.SetValue( True )
66 label
= wx
.StaticText( panel
, -1, "Resulting numeric control:" )
67 font
= label
.GetFont()
68 font
.SetWeight(wx
.BOLD
)
71 self
.target_ctl
= mnum
.MaskedNumCtrl( panel
, -1, name
="target control" )
73 label_numselect
= wx
.StaticText( panel
, -1, """\
74 Programmatically set the above
76 self
.numselect
= wx
.ComboBox(panel
, -1, choices
= [ '0', '111', '222.22', '-3', '54321.666666666', '-1353.978',
77 '1234567', '-1234567', '123456789', '-123456789.1',
78 '1234567890.', '-9876543210.9' ])
80 grid1
= wx
.FlexGridSizer( 0, 4, 0, 0 )
81 grid1
.Add( intlabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5)
82 grid1
.Add( self
.integerwidth
, 0, wx
.FIXED_SIZE|wx
.ALIGN_LEFT|wx
.ALL
, 5 )
84 grid1
.Add( groupcharlabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5)
85 grid1
.Add( self
.groupchar
, 0, wx
.FIXED_SIZE|wx
.ALIGN_LEFT|wx
.ALL
, 5 )
87 grid1
.Add( fraclabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
88 grid1
.Add( self
.fractionwidth
, 0, wx
.FIXED_SIZE|wx
.ALIGN_LEFT|wx
.ALL
, 5 )
90 grid1
.Add( decimalcharlabel
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5)
91 grid1
.Add( self
.decimalchar
, 0, wx
.FIXED_SIZE|wx
.ALIGN_LEFT|wx
.ALL
, 5 )
93 grid1
.Add( self
.set_min
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
94 grid1
.Add( self
.min, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
95 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
96 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
98 grid1
.Add( self
.set_max
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
99 grid1
.Add( self
.max, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
100 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
101 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
104 grid1
.Add( self
.limit_target
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
105 grid1
.Add( self
.allow_none
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
106 hbox1
= wx
.BoxSizer( wx
.HORIZONTAL
)
107 hbox1
.Add( (17,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
108 hbox1
.Add( self
.group_digits
, 0, wx
.ALIGN_LEFT|wx
.LEFT
, 5 )
109 grid1
.Add( hbox1
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
110 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
112 grid1
.Add( self
.allow_negative
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
113 grid1
.Add( self
.use_parens
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
114 hbox2
= wx
.BoxSizer( wx
.HORIZONTAL
)
115 hbox2
.Add( (17,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
116 hbox2
.Add( self
.select_on_entry
, 0, wx
.ALIGN_LEFT|wx
.LEFT
, 5 )
117 grid1
.Add( hbox2
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
118 grid1
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
121 grid2
= wx
.FlexGridSizer( 0, 2, 0, 0 )
122 grid2
.Add( label
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
123 grid2
.Add( self
.target_ctl
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
124 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
125 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
126 grid2
.Add( label_numselect
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
127 grid2
.Add( self
.numselect
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
128 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
129 grid2
.Add( (5,5), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
130 grid2
.AddGrowableCol(1)
132 self
.outer_box
= wx
.BoxSizer( wx
.VERTICAL
)
133 self
.outer_box
.Add(header
, 0, wx
.ALIGN_LEFT|wx
.TOP|wx
.LEFT
, 20)
134 self
.outer_box
.Add( grid1
, 0, wx
.ALIGN_CENTRE|wx
.LEFT|wx
.BOTTOM|wx
.RIGHT
, 20 )
135 self
.outer_box
.Add( grid2
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 20 )
138 panel
.SetAutoLayout( True )
139 panel
.SetSizer( self
.outer_box
)
140 self
.outer_box
.Fit( panel
)
141 panel
.Move( (50,10) )
144 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.OnSetIntWidth
, self
.integerwidth
)
145 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.OnSetFractionWidth
, self
.fractionwidth
)
146 self
.Bind(wx
.EVT_TEXT
, self
.OnSetGroupChar
, self
.groupchar
)
147 self
.Bind(wx
.EVT_TEXT
, self
.OnSetDecimalChar
, self
.decimalchar
)
149 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMin
, self
.set_min
)
150 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMax
, self
.set_max
)
151 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.SetTargetMinMax
, self
.min )
152 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.SetTargetMinMax
, self
.max )
154 self
.Bind(wx
.EVT_CHECKBOX
, self
.SetTargetMinMax
, self
.limit_target
)
155 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowNone
, self
.allow_none
)
156 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetGroupDigits
, self
.group_digits
)
157 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowNegative
, self
.allow_negative
)
158 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetUseParens
, self
.use_parens
)
159 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetSelectOnEntry
, self
.select_on_entry
)
161 self
.Bind(mnum
.EVT_MASKEDNUM
, self
.OnTargetChange
, self
.target_ctl
)
162 self
.Bind(wx
.EVT_COMBOBOX
, self
.OnNumberSelect
, self
.numselect
)
165 def OnSetIntWidth(self
, event
):
166 width
= self
.integerwidth
.GetValue()
169 self
.log
.write("integer width must be positive\n")
170 self
.integerwidth
.SetForegroundColour(wx
.RED
)
172 self
.integerwidth
.SetForegroundColour(wx
.BLACK
)
173 self
.log
.write("setting integer width to %d\n" % width
)
174 self
.target_ctl
.SetParameters( integerWidth
= width
)
175 # Now resize and fit the dialog as appropriate:
176 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
177 self
.outer_box
.Fit( self
.panel
)
178 self
.outer_box
.SetSizeHints( self
.panel
)
181 def OnSetFractionWidth(self
, event
):
182 width
= self
.fractionwidth
.GetValue()
183 self
.log
.write("setting fraction width to %d\n" % width
)
184 self
.target_ctl
.SetParameters( fractionWidth
= width
)
185 # Now resize and fit the dialog as appropriate:
186 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
187 self
.outer_box
.Fit( self
.panel
)
188 self
.outer_box
.SetSizeHints( self
.panel
)
191 def OnSetGroupChar( self
, event
):
192 char
= self
.groupchar
.GetValue()
193 if self
.target_ctl
.GetDecimalChar() == char
:
194 self
.log
.write("group and decimal chars must be different\n")
195 self
.groupchar
.SetForegroundColour(wx
.RED
)
197 self
.groupchar
.SetForegroundColour(wx
.BLACK
)
198 self
.log
.write("setting group char to %s\n" % char
)
199 self
.target_ctl
.SetGroupChar( char
)
201 def OnSetDecimalChar( self
, event
):
202 char
= self
.decimalchar
.GetValue()
203 if self
.target_ctl
.GetGroupChar() == char
:
204 self
.log
.write("group and decimal chars must be different\n")
205 self
.decimalchar
.SetForegroundColour(wx
.RED
)
207 self
.decimalchar
.SetForegroundColour(wx
.BLACK
)
208 self
.log
.write("setting decimal char to %s\n" % char
)
209 self
.target_ctl
.SetDecimalChar( char
)
212 def OnSetMin( self
, event
):
213 self
.min.Enable( self
.set_min
.GetValue() )
214 self
.SetTargetMinMax()
216 def OnSetMax( self
, event
):
217 self
.max.Enable( self
.set_max
.GetValue() )
218 self
.SetTargetMinMax()
221 def SetTargetMinMax( self
, event
=None ):
223 self
.target_ctl
.SetLimited( self
.limit_target
.GetValue() )
225 if self
.set_min
.GetValue():
226 min = self
.min.GetValue()
227 if self
.set_max
.GetValue():
228 max = self
.max.GetValue()
230 cur_min
, cur_max
= self
.target_ctl
.GetBounds()
232 if min != cur_min
and not self
.target_ctl
.SetMin( min ):
233 if self
.target_ctl
.GetMax() is None and cur_max
> min:
234 self
.log
.write( "min (%d) won't fit in control -- bound not set\n" % min )
236 self
.log
.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self
.target_ctl
.GetMax() ) )
237 self
.min.SetParameters( signedForegroundColour
=wx
.RED
, foregroundColour
=wx
.RED
)
239 self
.min.SetParameters( signedForegroundColour
=wx
.BLACK
, foregroundColour
=wx
.BLACK
)
242 if max != cur_max
and not self
.target_ctl
.SetMax( max ):
243 if self
.target_ctl
.GetMax() is None and cur_min
< max:
244 self
.log
.write( "max (%d) won't fit in control -- bound not set\n" % max )
246 self
.log
.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self
.target_ctl
.GetMin() ) )
247 self
.max.SetParameters( signedForegroundColour
=wx
.RED
, foregroundColour
=wx
.RED
)
249 self
.max.SetParameters( signedForegroundColour
=wx
.BLACK
, foregroundColour
=wx
.BLACK
)
252 if min != cur_min
or max != cur_max
:
253 new_min
, new_max
= self
.target_ctl
.GetBounds()
254 self
.log
.write( "current min, max: (%s, %s)\n" % ( str(new_min
), str(new_max
) ) )
257 def OnSetAllowNone( self
, event
):
258 self
.target_ctl
.SetAllowNone( self
.allow_none
.GetValue() )
261 def OnSetGroupDigits( self
, event
):
262 self
.target_ctl
.SetGroupDigits( self
.group_digits
.GetValue() )
263 # Now resize and fit the dialog as appropriate:
264 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
265 self
.outer_box
.Fit( self
.panel
)
266 self
.outer_box
.SetSizeHints( self
.panel
)
269 def OnSetAllowNegative( self
, event
):
270 if self
.allow_negative
.GetValue():
271 self
.use_parens
.Enable(True)
272 self
.target_ctl
.SetParameters(allowNegative
=True,
273 useParensForNegatives
= self
.use_parens
.GetValue())
275 self
.target_ctl
.SetAllowNegative(False)
276 # Now resize and fit the dialog as appropriate:
277 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
278 self
.outer_box
.Fit( self
.panel
)
279 self
.outer_box
.SetSizeHints( self
.panel
)
282 def OnSetUseParens( self
, event
):
283 self
.target_ctl
.SetUseParensForNegatives( self
.use_parens
.GetValue() )
284 # Now resize and fit the dialog as appropriate:
285 self
.grid2
.SetItemMinSize(self
.target_ctl
, self
.target_ctl
.GetSize())
286 self
.outer_box
.Fit( self
.panel
)
287 self
.outer_box
.SetSizeHints( self
.panel
)
290 def OnSetSelectOnEntry( self
, event
):
291 self
.target_ctl
.SetSelectOnEntry( self
.select_on_entry
.GetValue() )
294 def OnTargetChange( self
, event
):
295 ctl
= event
.GetEventObject()
296 value
= ctl
.GetValue()
297 ib_str
= [ " (out of bounds)", "" ]
298 self
.log
.write( "value = %s (%s)%s\n" % ( repr(value
), repr(type(value
)), ib_str
[ ctl
.IsInBounds(value
) ] ) )
301 def OnNumberSelect( self
, event
):
302 value
= event
.GetString()
304 if value
.find('.') != -1:
305 numvalue
= float(value
)
307 numvalue
= long(value
)
309 numvalue
= value
# try to clear the value again
312 self
.target_ctl
.SetValue(numvalue
)
314 type, value
, tb
= sys
.exc_info()
315 for line
in traceback
.format_exception_only(type, value
):
319 #----------------------------------------------------------------------
321 def runTest( frame
, nb
, log
):
322 win
= TestPanel( nb
, log
)
325 #----------------------------------------------------------------------
326 overview
= mnum
.__doc
__
328 if __name__
== '__main__':
331 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])