📄 maskededitcontrols.py
字号:
import string
import sys
import traceback
import wx
import wx.lib.masked as masked
import wx.lib.scrolledpanel as scroll
class demoMixin:
"""
Centralized routines common to demo pages, to remove repetition.
"""
def labelGeneralTable(self, sizer):
description = wx.StaticText( self, -1, "Description", )
mask = wx.StaticText( self, -1, "Mask Value" )
formatcode = wx.StaticText( self, -1, "Format" )
regex = wx.StaticText( self, -1, "Regexp Validator(opt.)" )
ctrl = wx.StaticText( self, -1, "Masked TextCtrl" )
description.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
mask.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
formatcode.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD) )
regex.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
ctrl.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD))
sizer.Add(description)
sizer.Add(mask)
sizer.Add(formatcode)
sizer.Add(regex)
sizer.Add(ctrl)
def layoutGeneralTable(self, controls, sizer):
for control in controls:
sizer.Add( wx.StaticText( self, -1, control[0]) )
sizer.Add( wx.StaticText( self, -1, control[1]) )
sizer.Add( wx.StaticText( self, -1, control[3]) )
sizer.Add( wx.StaticText( self, -1, control[4]) )
if control in controls:
newControl = masked.TextCtrl( self, -1, "",
mask = control[1],
excludeChars = control[2],
formatcodes = control[3],
includeChars = "",
validRegex = control[4],
validRange = control[5],
choices = control[6],
choiceRequired = True,
defaultValue = control[7],
demo = True,
name = control[0])
self.editList.append(newControl)
sizer.Add(newControl)
def changeControlParams(self, event, parameter, checked_value, notchecked_value):
if event.IsChecked(): value = checked_value
else: value = notchecked_value
kwargs = {parameter: value}
for control in self.editList:
control.SetCtrlParameters(**kwargs)
control.Refresh()
self.Refresh()
#----------------------------------------------------------------------------
class demoPage1(scroll.ScrolledPanel, demoMixin):
def __init__(self, parent, log):
scroll.ScrolledPanel.__init__(self, parent, -1)
self.sizer = wx.BoxSizer( wx.VERTICAL )
self.editList = []
label = wx.StaticText( self, -1, """\
Here are some basic masked TextCtrls to give you an idea of what you can do
with this control. Note that all controls have been auto-sized by including 'F' in
the format codes.
Try entering nonsensical or partial values in validated fields to see what happens.
Note that the State and Last Name fields are list-limited (valid last names are:
Smith, Jones, Williams). Signs on numbers can be toggled with the minus key.
""")
label.SetForegroundColour( "Blue" )
header = wx.BoxSizer( wx.HORIZONTAL )
header.Add( label, 0, flag=wx.ALIGN_LEFT|wx.ALL, border = 5 )
highlight = wx.CheckBox( self, -1, "Highlight Empty" )
disallow = wx.CheckBox( self, -1, "Disallow Empty" )
showFill = wx.CheckBox( self, -1, "change fillChar" )
vbox = wx.BoxSizer( wx.VERTICAL )
vbox.Add( highlight, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
vbox.Add( disallow, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
vbox.Add( showFill, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
header.Add((15, 0))
header.Add(vbox, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
self.Bind(wx.EVT_CHECKBOX, self.onHighlightEmpty, id=highlight.GetId())
self.Bind(wx.EVT_CHECKBOX, self.onDisallowEmpty, id=disallow.GetId())
self.Bind(wx.EVT_CHECKBOX, self.onShowFill, id=showFill.GetId())
grid = wx.FlexGridSizer( 0, 5, vgap=10, hgap=10 )
self.labelGeneralTable(grid)
# The following list is of the controls for the demo. Feel free to play around with
# the options!
controls = [
#description mask excl format regexp range,list,initial
("Phone No", "(###) ###-#### x:###", "", 'F^-', "^\(\d{3}\) \d{3}-\d{4}", '','',''),
("Social Sec#", "###-##-####", "", 'F', "\d{3}-\d{2}-\d{4}", '','',''),
("Full Name", "C{14}", "", 'F_', '^[A-Z][a-zA-Z]+ [A-Z][a-zA-Z]+', '','',''),
("Last Name Only", "C{14}", "", 'F {list}', '^[A-Z][a-zA-Z]+', '',('Smith','Jones','Williams'),''),
("Zip plus 4", "#{5}-#{4}", "", 'F', "\d{5}-(\s{4}|\d{4})", '','',''),
("Customer No", "\CAA-###", "", 'F!', "C[A-Z]{2}-\d{3}", '','',''),
("Invoice Total", "#{9}.##", "", 'F-_,', "", '','',''),
("Integer", "#{9}", "", 'F-_', "", '','',''),
]
self.layoutGeneralTable(controls, grid)
self.sizer.Add( header, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
self.sizer.Add( grid, 0, flag= wx.ALIGN_LEFT|wx.LEFT, border=5 )
self.SetSizer(self.sizer)
self.SetupScrolling()
self.SetAutoLayout(1)
def onDisallowEmpty( self, event ):
""" Set emptyInvalid parameter on/off """
self.changeControlParams( event, "emptyInvalid", True, False )
def onHighlightEmpty( self, event ):
""" Highlight empty values"""
self.changeControlParams( event, "emptyBackgroundColour", "Blue", "White" )
def onShowFill( self, event ):
""" Set fillChar parameter to '?' or ' ' """
self.changeControlParams( event, "fillChar", '?', ' ' )
class demoPage2(scroll.ScrolledPanel, demoMixin):
def __init__( self, parent, log ):
self.log = log
scroll.ScrolledPanel.__init__( self, parent, -1 )
self.sizer = wx.BoxSizer( wx.VERTICAL )
label = wx.StaticText( self, -1, """\
All these controls have been created by passing a single parameter, the autoformat code,
and use the factory class masked.Ctrl with its default controlType.
The masked package contains an internal dictionary of types and formats (autoformats).
Many of these already do complicated validation; To see some examples, try
29 Feb 2002 vs. 2004 for the date formats, or email address validation.
""")
label.SetForegroundColour( "Blue" )
self.sizer.Add( label, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
description = wx.StaticText( self, -1, "Description")
autofmt = wx.StaticText( self, -1, "AutoFormat Code")
ctrl = wx.StaticText( self, -1, "Masked Ctrl")
description.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
autofmt.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
ctrl.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
grid = wx.FlexGridSizer( 0, 3, vgap=10, hgap=5 )
grid.Add( description, 0, wx.ALIGN_LEFT )
grid.Add( autofmt, 0, wx.ALIGN_LEFT )
grid.Add( ctrl, 0, wx.ALIGN_LEFT )
for autoformat, desc in masked.autoformats:
grid.Add( wx.StaticText( self, -1, desc), 0, wx.ALIGN_LEFT )
grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
grid.Add( masked.Ctrl( self, -1, "",
autoformat = autoformat,
demo = True,
name = autoformat),
0, wx.ALIGN_LEFT )
self.sizer.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, border=5 )
self.SetSizer( self.sizer )
self.SetAutoLayout( 1 )
self.SetupScrolling()
class demoPage3(scroll.ScrolledPanel, demoMixin):
def __init__(self, parent, log):
self.log = log
scroll.ScrolledPanel.__init__(self, parent, -1)
self.sizer = wx.BoxSizer( wx.VERTICAL )
self.editList = []
label = wx.StaticText( self, -1, """\
Here masked TextCtrls that have default values. The states
control has a list of valid values, and the unsigned integer
has a legal range specified.
""")
label.SetForegroundColour( "Blue" )
requireValid = wx.CheckBox( self, -1, "Require Valid Value" )
self.Bind(wx.EVT_CHECKBOX, self.onRequireValid, id=requireValid.GetId())
header = wx.BoxSizer( wx.HORIZONTAL )
header.Add( label, 0, flag=wx.ALIGN_LEFT|wx.ALL, border = 5)
header.Add((75, 0))
header.Add( requireValid, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=10 )
grid = wx.FlexGridSizer( 0, 5, vgap=10, hgap=10 )
self.labelGeneralTable( grid )
controls = [
#description mask excl format regexp range,list,initial
("U.S. State (2 char)", "AA", "", 'F!_', "[A-Z]{2}", '', masked.states, masked.states[0]),
("Integer (signed)", "#{6}", "", 'F-_', "", '','', ' 0 '),
("Integer (unsigned)\n(1-399)","######", "", 'F_', "", (1,399),'', '1 '),
("Float (signed)", "#{6}.#{9}", "", 'F-_R', "", '','', '000000.000000000'),
("Date (MDY) + Time", "##/##/#### ##:##:## AM", 'BCDEFGHIJKLMNOQRSTUVWXYZ','DF!',"", '','', wx.DateTime_Now().Format("%m/%d/%Y %I:%M:%S %p")),
]
self.layoutGeneralTable( controls, grid )
self.sizer.Add( header, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
self.sizer.Add( grid, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 )
self.SetSizer( self.sizer )
self.SetAutoLayout( 1 )
self.SetupScrolling()
def onRequireValid( self, event ):
""" Set validRequired parameter on/off """
self.changeControlParams( event, "validRequired", True, False )
class demoPage4(scroll.ScrolledPanel, demoMixin):
def __init__( self, parent, log ):
self.log = log
scroll.ScrolledPanel.__init__( self, parent, -1 )
self.sizer = wx.BoxSizer( wx.VERTICAL )
label = wx.StaticText( self, -1, """\
These controls have field-specific choice lists and allow autocompletion.
Down arrow or Page Down in an uncompleted field with an auto-completable field will attempt
to auto-complete a field if it has a choice list.
Page Down and Shift-Down arrow will also auto-complete, or cycle through the complete list.
Page Up and Shift-Up arrow will similarly cycle backwards through the list.
""")
label.SetForegroundColour( "Blue" )
self.sizer.Add( label, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
description = wx.StaticText( self, -1, "Description" )
autofmt = wx.StaticText( self, -1, "AutoFormat Code" )
fields = wx.StaticText( self, -1, "Field Objects" )
ctrl = wx.StaticText( self, -1, "Masked TextCtrl" )
description.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
autofmt.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
fields.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
ctrl.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) )
grid = wx.FlexGridSizer( 0, 4, vgap=10, hgap=10 )
grid.Add( description, 0, wx.ALIGN_LEFT )
grid.Add( autofmt, 0, wx.ALIGN_LEFT )
grid.Add( fields, 0, wx.ALIGN_LEFT )
grid.Add( ctrl, 0, wx.ALIGN_LEFT )
autoformat = "USPHONEFULLEXT"
fieldsDict = {0: masked.Field(choices=["617","781","508","978","413"], choiceRequired=True)}
fieldsLabel = """\
{0: Field(choices=[
"617","781",
"508","978","413"],
choiceRequired=True)}"""
grid.Add( wx.StaticText( self, -1, "Restricted Area Code"), 0, wx.ALIGN_LEFT )
grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT )
grid.Add( masked.TextCtrl( self, -1, "",
autoformat = autoformat,
fields = fieldsDict,
demo = True,
name = autoformat),
0, wx.ALIGN_LEFT )
autoformat = "EXPDATEMMYY"
fieldsDict = {1: masked.Field(choices=["03", "04", "05"], choiceRequired=True)}
fieldsLabel = """\
{1: Field(choices=[
"03", "04", "05"],
choiceRequired=True)}"""
exp = masked.TextCtrl( self, -1, "",
autoformat = autoformat,
fields = fieldsDict,
demo = True,
name = autoformat)
grid.Add( wx.StaticText( self, -1, "Restricted Expiration"), 0, wx.ALIGN_LEFT )
grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT )
grid.Add( exp, 0, wx.ALIGN_LEFT )
fieldsDict = {0: masked.Field(choices=["02134","02155"], choiceRequired=True),
1: masked.Field(choices=["1234", "5678"], choiceRequired=False)}
fieldsLabel = """\
{0: Field(choices=["02134","02155"],
choiceRequired=True),
1: Field(choices=["1234", "5678"],
choiceRequired=False)}"""
autoformat = "USZIPPLUS4"
zip = masked.TextCtrl( self, -1, "",
autoformat = autoformat,
fields = fieldsDict,
demo = True,
name = autoformat)
grid.Add( wx.StaticText( self, -1, "Restricted Zip + 4"), 0, wx.ALIGN_LEFT )
grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT )
grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT )
grid.Add( zip, 0, wx.ALIGN_LEFT )
self.sizer.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, border=5 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -