| 1 | from wxPython.wx import * |
| 2 | from wxPython.lib.maskededit import Field, wxMaskedTextCtrl, wxMaskedComboBox, wxIpAddrCtrl, states, months |
| 3 | from wxPython.lib.maskededit import __doc__ as overviewdoc |
| 4 | from wxPython.lib.maskededit import autoformats |
| 5 | from wxPython.lib.scrolledpanel import wxScrolledPanel |
| 6 | import string, sys, traceback |
| 7 | |
| 8 | class demoMixin: |
| 9 | """ |
| 10 | Centralized routines common to demo pages, to remove repetition. |
| 11 | """ |
| 12 | def labelGeneralTable(self, sizer): |
| 13 | description = wxStaticText( self, -1, "Description", ) |
| 14 | mask = wxStaticText( self, -1, "Mask Value" ) |
| 15 | formatcode = wxStaticText( self, -1, "Format" ) |
| 16 | regex = wxStaticText( self, -1, "Regexp Validator(opt.)" ) |
| 17 | ctrl = wxStaticText( self, -1, "wxMaskedEdit Ctrl" ) |
| 18 | |
| 19 | description.SetFont( wxFont(9, wxSWISS, wxNORMAL, wxBOLD)) |
| 20 | mask.SetFont( wxFont(9, wxSWISS, wxNORMAL, wxBOLD)) |
| 21 | formatcode.SetFont( wxFont(9, wxSWISS, wxNORMAL, wxBOLD) ) |
| 22 | regex.SetFont( wxFont(9, wxSWISS, wxNORMAL, wxBOLD)) |
| 23 | ctrl.SetFont( wxFont(9, wxSWISS, wxNORMAL, wxBOLD)) |
| 24 | |
| 25 | sizer.Add(description) |
| 26 | sizer.Add(mask) |
| 27 | sizer.Add(formatcode) |
| 28 | sizer.Add(regex) |
| 29 | sizer.Add(ctrl) |
| 30 | |
| 31 | |
| 32 | def layoutGeneralTable(self, controls, sizer): |
| 33 | for control in controls: |
| 34 | sizer.Add( wxStaticText( self, -1, control[0]) ) |
| 35 | sizer.Add( wxStaticText( self, -1, control[1]) ) |
| 36 | sizer.Add( wxStaticText( self, -1, control[3]) ) |
| 37 | sizer.Add( wxStaticText( self, -1, control[4]) ) |
| 38 | |
| 39 | if control in controls: |
| 40 | newControl = wxMaskedTextCtrl( self, -1, "", |
| 41 | mask = control[1], |
| 42 | excludeChars = control[2], |
| 43 | formatcodes = control[3], |
| 44 | includeChars = "", |
| 45 | validRegex = control[4], |
| 46 | validRange = control[5], |
| 47 | choices = control[6], |
| 48 | choiceRequired = True, |
| 49 | defaultValue = control[7], |
| 50 | demo = True, |
| 51 | name = control[0]) |
| 52 | self.editList.append(newControl) |
| 53 | sizer.Add(newControl) |
| 54 | |
| 55 | |
| 56 | def changeControlParams(self, event, parameter, checked_value, notchecked_value): |
| 57 | if event.Checked(): value = checked_value |
| 58 | else: value = notchecked_value |
| 59 | kwargs = {parameter: value} |
| 60 | for control in self.editList: |
| 61 | control.SetCtrlParameters(**kwargs) |
| 62 | control.Refresh() |
| 63 | self.Refresh() |
| 64 | |
| 65 | |
| 66 | |
| 67 | #---------------------------------------------------------------------------- |
| 68 | class demoPage1(wxScrolledPanel, demoMixin): |
| 69 | def __init__(self, parent, log): |
| 70 | wxScrolledPanel.__init__(self, parent, -1) |
| 71 | self.sizer = wxBoxSizer( wxVERTICAL ) |
| 72 | self.editList = [] |
| 73 | |
| 74 | label = wxStaticText( self, -1, """\ |
| 75 | Here are some basic wxMaskedTextCtrls to give you an idea of what you can do |
| 76 | with this control. Note that all controls have been auto-sized by including 'F' in |
| 77 | the format codes. |
| 78 | |
| 79 | Try entering nonsensical or partial values in validated fields to see what happens. |
| 80 | Note that the State and Last Name fields are list-limited (valid last names are: |
| 81 | Smith, Jones, Williams). Signs on numbers can be toggled with the minus key. |
| 82 | """) |
| 83 | label.SetForegroundColour( "Blue" ) |
| 84 | header = wxBoxSizer( wxHORIZONTAL ) |
| 85 | header.Add( label, 0, flag=wxALIGN_LEFT|wxALL, border = 5 ) |
| 86 | |
| 87 | highlight = wxCheckBox( self, -1, "Highlight Empty" ) |
| 88 | disallow = wxCheckBox( self, -1, "Disallow Empty" ) |
| 89 | showFill = wxCheckBox( self, -1, "change fillChar" ) |
| 90 | |
| 91 | vbox = wxBoxSizer( wxVERTICAL ) |
| 92 | vbox.Add( highlight, 0, wxALIGN_LEFT|wxALL, 5 ) |
| 93 | vbox.Add( disallow, 0, wxALIGN_LEFT|wxALL, 5 ) |
| 94 | vbox.Add( showFill, 0, wxALIGN_LEFT|wxALL, 5 ) |
| 95 | header.AddSpacer(15, 0) |
| 96 | header.Add(vbox, 0, flag=wxALIGN_LEFT|wxALL, border=5 ) |
| 97 | |
| 98 | EVT_CHECKBOX( self, highlight.GetId(), self.onHighlightEmpty ) |
| 99 | EVT_CHECKBOX( self, disallow.GetId(), self.onDisallowEmpty ) |
| 100 | EVT_CHECKBOX( self, showFill.GetId(), self.onShowFill ) |
| 101 | |
| 102 | grid = wxFlexGridSizer( 0, 5, vgap=10, hgap=10 ) |
| 103 | self.labelGeneralTable(grid) |
| 104 | |
| 105 | # The following list is of the controls for the demo. Feel free to play around with |
| 106 | # the options! |
| 107 | controls = [ |
| 108 | #description mask excl format regexp range,list,initial |
| 109 | ("Phone No", "(###) ###-#### x:###", "", 'F^-', "^\(\d{3}\) \d{3}-\d{4}", '','',''), |
| 110 | ("Social Sec#", "###-##-####", "", 'F', "\d{3}-\d{2}-\d{4}", '','',''), |
| 111 | ("Full Name", "C{14}", "", 'F_', '^[A-Z][a-zA-Z]+ [A-Z][a-zA-Z]+', '','',''), |
| 112 | ("Last Name Only", "C{14}", "", 'F {list}', '^[A-Z][a-zA-Z]+', '',('Smith','Jones','Williams'),''), |
| 113 | ("Zip plus 4", "#{5}-#{4}", "", 'F', "\d{5}-(\s{4}|\d{4})", '','',''), |
| 114 | ("Customer No", "\CAA-###", "", 'F!', "C[A-Z]{2}-\d{3}", '','',''), |
| 115 | ("Invoice Total", "#{9}.##", "", 'F-_,', "", '','',''), |
| 116 | ("Integer", "#{9}", "", 'F-_', "", '','',''), |
| 117 | ] |
| 118 | |
| 119 | self.layoutGeneralTable(controls, grid) |
| 120 | self.sizer.Add( header, 0, flag=wxALIGN_LEFT|wxALL, border=5 ) |
| 121 | self.sizer.Add( grid, 0, flag= wxALIGN_LEFT|wxLEFT, border=5 ) |
| 122 | self.SetSizer(self.sizer) |
| 123 | self.SetupScrolling() |
| 124 | self.SetAutoLayout(1) |
| 125 | |
| 126 | |
| 127 | def onDisallowEmpty( self, event ): |
| 128 | """ Set emptyInvalid parameter on/off """ |
| 129 | self.changeControlParams( event, "emptyInvalid", True, False ) |
| 130 | |
| 131 | def onHighlightEmpty( self, event ): |
| 132 | """ Highlight empty values""" |
| 133 | self.changeControlParams( event, "emptyBackgroundColor", "Blue", "White" ) |
| 134 | |
| 135 | def onShowFill( self, event ): |
| 136 | """ Set fillChar parameter to '?' or ' ' """ |
| 137 | self.changeControlParams( event, "fillChar", '?', ' ' ) |
| 138 | |
| 139 | |
| 140 | class demoPage2(wxScrolledPanel, demoMixin): |
| 141 | def __init__( self, parent, log ): |
| 142 | self.log = log |
| 143 | wxScrolledPanel.__init__( self, parent, -1 ) |
| 144 | self.sizer = wxBoxSizer( wxVERTICAL ) |
| 145 | |
| 146 | label = wxStaticText( self, -1, """\ |
| 147 | All these controls have been created by passing a single parameter, the autoformat code. |
| 148 | The class contains an internal dictionary of types and formats (autoformats). |
| 149 | Many of these already do complicated validation; To see some examples, try |
| 150 | 29 Feb 2002 vs. 2004 for the date formats, or email address validation. |
| 151 | """) |
| 152 | |
| 153 | label.SetForegroundColour( "Blue" ) |
| 154 | self.sizer.Add( label, 0, wxALIGN_LEFT|wxALL, 5 ) |
| 155 | |
| 156 | description = wxStaticText( self, -1, "Description") |
| 157 | autofmt = wxStaticText( self, -1, "AutoFormat Code") |
| 158 | ctrl = wxStaticText( self, -1, "wxMaskedEdit Control") |
| 159 | |
| 160 | description.SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) ) |
| 161 | autofmt.SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) ) |
| 162 | ctrl.SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) ) |
| 163 | |
| 164 | grid = wxFlexGridSizer( 0, 3, vgap=10, hgap=5 ) |
| 165 | grid.Add( description, 0, wxALIGN_LEFT ) |
| 166 | grid.Add( autofmt, 0, wxALIGN_LEFT ) |
| 167 | grid.Add( ctrl, 0, wxALIGN_LEFT ) |
| 168 | |
| 169 | for autoformat, desc in autoformats: |
| 170 | grid.Add( wxStaticText( self, -1, desc), 0, wxALIGN_LEFT ) |
| 171 | grid.Add( wxStaticText( self, -1, autoformat), 0, wxALIGN_LEFT ) |
| 172 | grid.Add( wxMaskedTextCtrl( self, -1, "", |
| 173 | autoformat = autoformat, |
| 174 | demo = True, |
| 175 | name = autoformat), |
| 176 | 0, wxALIGN_LEFT ) |
| 177 | |
| 178 | self.sizer.Add( grid, 0, wxALIGN_LEFT|wxALL, border=5 ) |
| 179 | self.SetSizer( self.sizer ) |
| 180 | self.SetAutoLayout( 1 ) |
| 181 | self.SetupScrolling() |
| 182 | |
| 183 | |
| 184 | class demoPage3(wxScrolledPanel, demoMixin): |
| 185 | def __init__(self, parent, log): |
| 186 | self.log = log |
| 187 | wxScrolledPanel.__init__(self, parent, -1) |
| 188 | self.sizer = wxBoxSizer( wxVERTICAL ) |
| 189 | self.editList = [] |
| 190 | |
| 191 | label = wxStaticText( self, -1, """\ |
| 192 | Here wxMaskedTextCtrls that have default values. The states |
| 193 | control has a list of valid values, and the unsigned integer |
| 194 | has a legal range specified. |
| 195 | """) |
| 196 | label.SetForegroundColour( "Blue" ) |
| 197 | requireValid = wxCheckBox( self, -1, "Require Valid Value" ) |
| 198 | EVT_CHECKBOX( self, requireValid.GetId(), self.onRequireValid ) |
| 199 | |
| 200 | header = wxBoxSizer( wxHORIZONTAL ) |
| 201 | header.Add( label, 0, flag=wxALIGN_LEFT|wxALL, border = 5) |
| 202 | header.AddSpacer(75, 0) |
| 203 | header.Add( requireValid, 0, flag=wxALIGN_LEFT|wxALL, border=10 ) |
| 204 | |
| 205 | grid = wxFlexGridSizer( 0, 5, vgap=10, hgap=10 ) |
| 206 | self.labelGeneralTable( grid ) |
| 207 | |
| 208 | controls = [ |
| 209 | #description mask excl format regexp range,list,initial |
| 210 | ("U.S. State (2 char)", "AA", "", 'F!_', "[A-Z]{2}", '',states, states[0]), |
| 211 | ("Integer (signed)", "#{6}", "", 'F-_R', "", '','', '0 '), |
| 212 | ("Integer (unsigned)\n(1-399)","######", "", 'F_', "", (1,399),'', '1 '), |
| 213 | ("Float (signed)", "#{6}.#{9}", "", 'F-_R', "", '','', '000000.000000000'), |
| 214 | ("Date (MDY) + Time", "##/##/#### ##:##:## AM", 'BCDEFGHIJKLMNOQRSTUVWXYZ','DF!',"", '','', wxDateTime_Now().Format("%m/%d/%Y %I:%M:%S %p")), |
| 215 | ] |
| 216 | self.layoutGeneralTable( controls, grid ) |
| 217 | |
| 218 | self.sizer.Add( header, 0, flag=wxALIGN_LEFT|wxALL, border=5 ) |
| 219 | self.sizer.Add( grid, 0, flag=wxALIGN_LEFT|wxALL, border=5 ) |
| 220 | |
| 221 | self.SetSizer( self.sizer ) |
| 222 | self.SetAutoLayout( 1 ) |
| 223 | self.SetupScrolling() |
| 224 | |
| 225 | def onRequireValid( self, event ): |
| 226 | """ Set validRequired parameter on/off """ |
| 227 | self.changeControlParams( event, "validRequired", True, False ) |
| 228 | |
| 229 | |
| 230 | class demoPage4(wxScrolledPanel, demoMixin): |
| 231 | def __init__( self, parent, log ): |
| 232 | self.log = log |
| 233 | wxScrolledPanel.__init__( self, parent, -1 ) |
| 234 | self.sizer = wxBoxSizer( wxVERTICAL ) |
| 235 | |
| 236 | label = wxStaticText( self, -1, """\ |
| 237 | These controls have field-specific choice lists and allow autocompletion. |
| 238 | |
| 239 | Down arrow or Page Down in an uncompleted field with an auto-completable field will attempt |
| 240 | to auto-complete a field if it has a choice list. |
| 241 | Page Down and Shift-Down arrow will also auto-complete, or cycle through the complete list. |
| 242 | Page Up and Shift-Up arrow will similarly cycle backwards through the list. |
| 243 | """) |
| 244 | |
| 245 | label.SetForegroundColour( "Blue" ) |
| 246 | self.sizer.Add( label, 0, wxALIGN_LEFT|wxALL, 5 ) |
| 247 | |
| 248 | description = wxStaticText( self, -1, "Description" ) |
| 249 | autofmt = wxStaticText( self, -1, "AutoFormat Code" ) |
| 250 | fields = wxStaticText( self, -1, "Field Objects" ) |
| 251 | ctrl = wxStaticText( self, -1, "wxMaskedEdit Control" ) |
| 252 | |
| 253 | description.SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) ) |
| 254 | autofmt.SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) ) |
| 255 | fields.SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) ) |
| 256 | ctrl.SetFont( wxFont( 9, wxSWISS, wxNORMAL, wxBOLD ) ) |
| 257 | |
| 258 | grid = wxFlexGridSizer( 0, 4, vgap=10, hgap=10 ) |
| 259 | grid.Add( description, 0, wxALIGN_LEFT ) |
| 260 | grid.Add( autofmt, 0, wxALIGN_LEFT ) |
| 261 | grid.Add( fields, 0, wxALIGN_LEFT ) |
| 262 | grid.Add( ctrl, 0, wxALIGN_LEFT ) |
| 263 | |
| 264 | autoformat = "USPHONEFULLEXT" |
| 265 | fieldsDict = {0: Field(choices=["617","781","508","978","413"], choiceRequired=True)} |
| 266 | fieldsLabel = """\ |
| 267 | {0: Field(choices=[ |
| 268 | "617","781", |
| 269 | "508","978","413"], |
| 270 | choiceRequired=True)}""" |
| 271 | grid.Add( wxStaticText( self, -1, "Restricted Area Code"), 0, wxALIGN_LEFT ) |
| 272 | grid.Add( wxStaticText( self, -1, autoformat), 0, wxALIGN_LEFT ) |
| 273 | grid.Add( wxStaticText( self, -1, fieldsLabel), 0, wxALIGN_LEFT ) |
| 274 | grid.Add( wxMaskedTextCtrl( self, -1, "", |
| 275 | autoformat = autoformat, |
| 276 | fields = fieldsDict, |
| 277 | demo = True, |
| 278 | name = autoformat), |
| 279 | 0, wxALIGN_LEFT ) |
| 280 | |
| 281 | autoformat = "EXPDATEMMYY" |
| 282 | fieldsDict = {1: Field(choices=["03", "04", "05"], choiceRequired=True)} |
| 283 | fieldsLabel = """\ |
| 284 | {1: Field(choices=[ |
| 285 | "03", "04", "05"], |
| 286 | choiceRequired=True)}""" |
| 287 | exp = wxMaskedTextCtrl( self, -1, "", |
| 288 | autoformat = autoformat, |
| 289 | fields = fieldsDict, |
| 290 | demo = True, |
| 291 | name = autoformat) |
| 292 | |
| 293 | grid.Add( wxStaticText( self, -1, "Restricted Expiration"), 0, wxALIGN_LEFT ) |
| 294 | grid.Add( wxStaticText( self, -1, autoformat), 0, wxALIGN_LEFT ) |
| 295 | grid.Add( wxStaticText( self, -1, fieldsLabel), 0, wxALIGN_LEFT ) |
| 296 | grid.Add( exp, 0, wxALIGN_LEFT ) |
| 297 | |
| 298 | fieldsDict = {0: Field(choices=["02134","02155"], choiceRequired=True), |
| 299 | 1: Field(choices=["1234", "5678"], choiceRequired=False)} |
| 300 | fieldsLabel = """\ |
| 301 | {0: Field(choices=["02134","02155"], |
| 302 | choiceRequired=True), |
| 303 | 1: Field(choices=["1234", "5678"], |
| 304 | choiceRequired=False)}""" |
| 305 | autoformat = "USZIPPLUS4" |
| 306 | zip = wxMaskedTextCtrl( self, -1, "", |
| 307 | autoformat = autoformat, |
| 308 | fields = fieldsDict, |
| 309 | demo = True, |
| 310 | name = autoformat) |
| 311 | |
| 312 | grid.Add( wxStaticText( self, -1, "Restricted Zip + 4"), 0, wxALIGN_LEFT ) |
| 313 | grid.Add( wxStaticText( self, -1, autoformat), 0, wxALIGN_LEFT ) |
| 314 | grid.Add( wxStaticText( self, -1, fieldsLabel), 0, wxALIGN_LEFT ) |
| 315 | grid.Add( zip, 0, wxALIGN_LEFT ) |
| 316 | |
| 317 | self.sizer.Add( grid, 0, wxALIGN_LEFT|wxALL, border=5 ) |
| 318 | self.SetSizer( self.sizer ) |
| 319 | self.SetAutoLayout(1) |
| 320 | self.SetupScrolling() |
| 321 | |
| 322 | |
| 323 | class demoPage5(wxScrolledPanel, demoMixin): |
| 324 | def __init__( self, parent, log ): |
| 325 | self.log = log |
| 326 | wxScrolledPanel.__init__( self, parent, -1 ) |
| 327 | self.sizer = wxBoxSizer( wxVERTICAL ) |
| 328 | label = wxStaticText( self, -1, """\ |
| 329 | These are examples of wxMaskedComboBox and wxIpAddrCtrl, and more useful |
| 330 | configurations of a wxMaskedTextCtrl for integer and floating point input. |
| 331 | """) |
| 332 | label.SetForegroundColour( "Blue" ) |
| 333 | self.sizer.Add( label, 0, wxALIGN_LEFT|wxALL, 5 ) |
| 334 | |
| 335 | numerators = [ str(i) for i in range(1, 4) ] |
| 336 | denominators = [ string.ljust(str(i), 2) for i in [2,3,4,5,8,16,32,64] ] |
| 337 | fieldsDict = {0: Field(choices=numerators, choiceRequired=False), |
| 338 | 1: Field(choices=denominators, choiceRequired=True)} |
| 339 | choices = [] |
| 340 | for n in numerators: |
| 341 | for d in denominators: |
| 342 | if n != d: |
| 343 | choices.append( '%s/%s' % (n,d) ) |
| 344 | |
| 345 | |
| 346 | text1 = wxStaticText( self, -1, """\ |
| 347 | A masked ComboBox for fraction selection. |
| 348 | Choices for each side of the fraction can be |
| 349 | selected with PageUp/Down:""") |
| 350 | |
| 351 | fraction = wxMaskedComboBox( self, -1, "", |
| 352 | choices = choices, |
| 353 | choiceRequired = True, |
| 354 | mask = "#/##", |
| 355 | formatcodes = "F_", |
| 356 | validRegex = "^\d\/\d\d?", |
| 357 | fields = fieldsDict ) |
| 358 | |
| 359 | |
| 360 | text2 = wxStaticText( self, -1, """ |
| 361 | A masked ComboBox to validate |
| 362 | text from a list of numeric codes:""") |
| 363 | |
| 364 | choices = ["91", "136", "305", "4579"] |
| 365 | code = wxMaskedComboBox( self, -1, choices[0], |
| 366 | choices = choices, |
| 367 | choiceRequired = True, |
| 368 | formatcodes = "F_r", |
| 369 | mask = "####") |
| 370 | |
| 371 | |
| 372 | text3 = wxStaticText( self, -1, """\ |
| 373 | A masked state selector; only "legal" values |
| 374 | can be entered:""") |
| 375 | |
| 376 | state = wxMaskedComboBox( self, -1, states[0], |
| 377 | choices = states, |
| 378 | autoformat="USSTATE") |
| 379 | |
| 380 | text4 = wxStaticText( self, -1, "An empty IP Address entry control:") |
| 381 | ip_addr1 = wxIpAddrCtrl( self, -1, style = wxTE_PROCESS_TAB ) |
| 382 | |
| 383 | |
| 384 | text5 = wxStaticText( self, -1, "An IP Address control with a restricted mask:") |
| 385 | ip_addr2 = wxIpAddrCtrl( self, -1, mask=" 10. 1.109.###" ) |
| 386 | |
| 387 | |
| 388 | text6 = wxStaticText( self, -1, """\ |
| 389 | An IP Address control with restricted choices |
| 390 | of form: 10. (1|2) . (129..255) . (0..255)""") |
| 391 | ip_addr3 = wxIpAddrCtrl( self, -1, mask=" 10. #.###.###") |
| 392 | ip_addr3.SetFieldParameters(0, validRegex="1|2" ) # requires entry to match or not allowed |
| 393 | |
| 394 | |
| 395 | # This allows any value in penultimate field, but colors anything outside of the range invalid: |
| 396 | ip_addr3.SetFieldParameters(1, validRange=(129,255), validRequired=False ) |
| 397 | |
| 398 | text7 = wxStaticText( self, -1, """\ |
| 399 | A right-insert integer entry control:""") |
| 400 | intctrl = wxMaskedTextCtrl(self, -1, name='intctrl', mask="#{9}", formatcodes = '_-r,F') |
| 401 | |
| 402 | text8 = wxStaticText( self, -1, """\ |
| 403 | A floating point entry control |
| 404 | with right-insert for ordinal:""") |
| 405 | self.floatctrl = wxMaskedTextCtrl(self, -1, name='floatctrl', mask="#{9}.#{2}", formatcodes="F,_-R") |
| 406 | self.floatctrl.SetFieldParameters(0, formatcodes='r<', validRequired=True) # right-insert, require explicit cursor movement to change fields |
| 407 | self.floatctrl.SetFieldParameters(1, defaultValue='00') # don't allow blank fraction |
| 408 | |
| 409 | text9 = wxStaticText( self, -1, """\ |
| 410 | Use this control to programmatically set |
| 411 | the value of the above float control:""") |
| 412 | number_combo = wxComboBox(self, -1, choices = [ '', '111', '222.22', '-3', '54321.666666666', '-1353.978', |
| 413 | '1234567', '-1234567', '123456789', '-123456789.1', |
| 414 | '1234567890.', '-1234567890.1' ]) |
| 415 | |
| 416 | grid = wxFlexGridSizer( 0, 2, vgap=10, hgap = 5 ) |
| 417 | grid.Add( text1, 0, wxALIGN_LEFT ) |
| 418 | grid.Add( fraction, 0, wxALIGN_LEFT ) |
| 419 | grid.Add( text2, 0, wxALIGN_LEFT ) |
| 420 | grid.Add( code, 0, wxALIGN_LEFT ) |
| 421 | grid.Add( text3, 0, wxALIGN_LEFT ) |
| 422 | grid.Add( state, 0, wxALIGN_LEFT ) |
| 423 | grid.Add( text4, 0, wxALIGN_LEFT ) |
| 424 | grid.Add( ip_addr1, 0, wxALIGN_LEFT ) |
| 425 | grid.Add( text5, 0, wxALIGN_LEFT ) |
| 426 | grid.Add( ip_addr2, 0, wxALIGN_LEFT ) |
| 427 | grid.Add( text6, 0, wxALIGN_LEFT ) |
| 428 | grid.Add( ip_addr3, 0, wxALIGN_LEFT ) |
| 429 | grid.Add( text7, 0, wxALIGN_LEFT ) |
| 430 | grid.Add( intctrl, 0, wxALIGN_LEFT ) |
| 431 | grid.Add( text8, 0, wxALIGN_LEFT ) |
| 432 | grid.Add( self.floatctrl, 0, wxALIGN_LEFT ) |
| 433 | grid.Add( text9, 0, wxALIGN_LEFT ) |
| 434 | grid.Add( number_combo, 0, wxALIGN_LEFT ) |
| 435 | |
| 436 | self.sizer.Add( grid, 0, wxALIGN_LEFT|wxALL, border=5 ) |
| 437 | self.SetSizer( self.sizer ) |
| 438 | self.SetAutoLayout(1) |
| 439 | self.SetupScrolling() |
| 440 | |
| 441 | EVT_COMBOBOX( self, fraction.GetId(), self.OnComboChange ) |
| 442 | EVT_COMBOBOX( self, code.GetId(), self.OnComboChange ) |
| 443 | EVT_COMBOBOX( self, state.GetId(), self.OnComboChange ) |
| 444 | EVT_TEXT( self, fraction.GetId(), self.OnComboChange ) |
| 445 | EVT_TEXT( self, code.GetId(), self.OnComboChange ) |
| 446 | EVT_TEXT( self, state.GetId(), self.OnComboChange ) |
| 447 | |
| 448 | EVT_TEXT( self, ip_addr1.GetId(), self.OnIpAddrChange ) |
| 449 | EVT_TEXT( self, ip_addr2.GetId(), self.OnIpAddrChange ) |
| 450 | EVT_TEXT( self, ip_addr3.GetId(), self.OnIpAddrChange ) |
| 451 | EVT_TEXT( self, intctrl.GetId(), self.OnTextChange ) |
| 452 | EVT_TEXT( self, self.floatctrl.GetId(), self.OnTextChange ) |
| 453 | EVT_COMBOBOX( self, number_combo.GetId(), self.OnNumberSelect ) |
| 454 | |
| 455 | |
| 456 | def OnComboChange( self, event ): |
| 457 | ctl = self.FindWindowById( event.GetId() ) |
| 458 | if not ctl.IsValid(): |
| 459 | self.log.write('current value not a valid choice') |
| 460 | |
| 461 | def OnIpAddrChange( self, event ): |
| 462 | ip_addr = self.FindWindowById( event.GetId() ) |
| 463 | if ip_addr.IsValid(): |
| 464 | self.log.write('new addr = %s\n' % ip_addr.GetAddress() ) |
| 465 | |
| 466 | def OnTextChange( self, event ): |
| 467 | ctl = self.FindWindowById( event.GetId() ) |
| 468 | if ctl.IsValid(): |
| 469 | self.log.write('new value = %s\n' % ctl.GetValue() ) |
| 470 | |
| 471 | def OnNumberSelect( self, event ): |
| 472 | value = event.GetString() |
| 473 | |
| 474 | # Format choice to fit into format for #{9}.#{2}, with sign position reserved: |
| 475 | # (ordinal + fraction == 11 + decimal point + sign == 13) |
| 476 | # |
| 477 | # Note: since self.floatctrl a right-aligned control, you could also just use |
| 478 | # "%.2f", but this wouldn't work properly for a left-aligned control. |
| 479 | # (See .SetValue() documentation in Overview.) |
| 480 | # |
| 481 | if value: |
| 482 | floattext = "%13.2f" % float(value) |
| 483 | else: |
| 484 | floattext = value # clear the value again |
| 485 | try: |
| 486 | self.floatctrl.SetValue(floattext) |
| 487 | except: |
| 488 | type, value, tb = sys.exc_info() |
| 489 | for line in traceback.format_exception_only(type, value): |
| 490 | self.log.write(line) |
| 491 | |
| 492 | # --------------------------------------------------------------------- |
| 493 | class TestMaskedTextCtrls(wxNotebook): |
| 494 | def __init__(self, parent, id, log): |
| 495 | wxNotebook.__init__(self, parent, id) |
| 496 | self.log = log |
| 497 | |
| 498 | win = demoPage1(self, log) |
| 499 | self.AddPage(win, "General examples") |
| 500 | |
| 501 | win = demoPage2(self, log) |
| 502 | self.AddPage(win, 'Auto-formatted controls') |
| 503 | |
| 504 | win = demoPage3(self, log) |
| 505 | self.AddPage(win, "Using default values") |
| 506 | |
| 507 | win = demoPage4(self, log) |
| 508 | self.AddPage(win, 'Using auto-complete fields') |
| 509 | |
| 510 | win = demoPage5(self, log) |
| 511 | self.AddPage(win, 'Other masked controls') |
| 512 | |
| 513 | |
| 514 | #---------------------------------------------------------------------------- |
| 515 | |
| 516 | def runTest(frame, nb, log): |
| 517 | testWin = TestMaskedTextCtrls(nb, -1, log) |
| 518 | return testWin |
| 519 | |
| 520 | def RunStandalone(): |
| 521 | app = wxPySimpleApp() |
| 522 | frame = wxFrame(None, -1, "Test wxMaskedTextCtrl", size=(640, 480)) |
| 523 | win = TestMaskedTextCtrls(frame, -1, sys.stdout) |
| 524 | frame.Show(True) |
| 525 | app.MainLoop() |
| 526 | #---------------------------------------------------------------------------- |
| 527 | if __name__ == "__main__": |
| 528 | RunStandalone() |
| 529 | |
| 530 | |
| 531 | overview = """<html> |
| 532 | <PRE><FONT SIZE=-1> |
| 533 | """ + overviewdoc + """ |
| 534 | </FONT></PRE> |
| 535 | """ |
| 536 | |
| 537 | if __name__ == "__main__": |
| 538 | import sys,os |
| 539 | import run |
| 540 | run.main(['', os.path.basename(sys.argv[0])]) |