在Char Validator中允许TAB

时间:2016-01-04 16:43:04

标签: python-2.7 validation wxpython

目前我有一个数据输入屏幕,用户必须使用鼠标在输入字段之间导航。我想添加一个TAB工具,以便他们可以在输入字段之间进行TAB,例如姓氏→名字→DOB等输入屏幕使用验证器来防止用户输入废话: -

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.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  == ALPHA_NUMERIC and key not in string.printable:
        return
    evt.Skip()

我已添加: -

style=wx.TAB_TRAVERSAL | wx.DEFAULT_DIALOG_STYLE

到框架。但是Validator会捕获TAB。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

由于您要将密钥转换为chr(),因此无法识别wx.WXK_TAB,请参阅以下内容以证明您的问题:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class Example(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, wx.ID_ANY, "Tab Key Test",size=(310,120))
        self.Stop_prog = False
        self.top = wx.Panel(self, wx.ID_ANY)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.text_input = wx.TextCtrl(self.top, wx.ID_ANY, size=(300,25))
        self.sizer.Add(self.text_input)
        self.text_input.Bind(wx.EVT_KEY_DOWN, self.OnKey)
        self.top.Sizer = self.sizer
        self.top.Sizer.Fit(self.top)
        self.Show()

    def OnKey(self,event):
        print "Key Pressed", event.GetKeyCode()
        keycode = event.GetKeyCode()
        try:
            charkey = chr(event.GetKeyCode())
            print "Character Key", charkey
        except:
            print "unable to convert key to a character in the range 256"
        if keycode == wx.WXK_F1: 
            self.Close()
        elif keycode == wx.WXK_TAB:
            print "TAB"
            self.text_input.SetValue("You pressed Tab")
        else:
            event.Skip()

def main():
    ex = wx.App()
    Example(None)
    ex.MainLoop()
if __name__ == '__main__':
    main()