在用户输入中检查BS(退格)

时间:2014-10-29 10:29:05

标签: python python-2.7 wxpython

以下是我用来验证用户输入各种textCtrl字段的例程:

def OnChar(self, evt):
    key = chr(evt.GetKeyCode())
    if self.flag  == LETTERS and key not in string.letters:
        return
    if self.flag  == DECIMAL_DIGITS and key not in string.digits:
        return
    if self.flag  == HEX_DIGITS and key not in string.hexdigits:
        return
    if self.flag  == NUMERIC and key not in '0123456789.':
        return
    if self.flag  == DATE_TIME and key not in '0123456789/: ':
        return
    evt.Skip()

我遇到的问题是,一旦你开始键入数据,它就不允许你输入BS。是否有相对简单的方法允许这个?

谢谢...

1 个答案:

答案 0 :(得分:0)

您可以只过滤这些键并跳过事件以确保评估退格(或其他功能键):

def OnChar(self, evt):

    key = chr(evt.GetKeyCode())
    if ord(key) in [wx.WXK_BACK, wx.WXK_DELETE]:
        evt.Skip()
    if self.flag  == LETTERS and key not in string.ascii_letters:
        return
    if self.flag  == DECIMAL_DIGITS and key not in string.digits:
        return
    if self.flag  == HEX_DIGITS and key not in string.hexdigits:
        return
    if self.flag  == NUMERIC and key not in '0123456789.':
        return
    if self.flag  == DATE_TIME and key not in '0123456789/: ':
        return
    evt.Skip()

您可以在wx Python KeyEvent Documentation

中查看这些密码代码的名称

Lokla