如何将wx.TextCtrl中的字母输入转换为所有大写字母?

时间:2012-04-08 14:24:53

标签: python validation wxpython

我正在尝试根据this response to another question on SO编写wx.TextCtrl的第一个验证程序,该验证程序应执行以下操作:

  

所有字母都应打印为字母,文字字段应仅包含字母和数字。

然而,我的尝试在两个问题上失败了:

  • 当我更改模板(取自linked response)时,文本字段中没有添加任何内容。

  • 我不能将小写字母改为大写字母。

这是我的尝试:

import wx
import string

########################################################################
class CharValidator(wx.PyValidator):
    ''' Validates data as it is entered into the text controls. '''

    #----------------------------------------------------------------------
    def __init__(self, flag):
        wx.PyValidator.__init__(self)
        self.flag = flag
        self.Bind(wx.EVT_CHAR, self.OnChar)

    #----------------------------------------------------------------------
    def Clone(self):
        '''Required Validator method'''
        return CharValidator(self.flag)

    #----------------------------------------------------------------------
    def Validate(self, win):
        return True

    #----------------------------------------------------------------------
    def TransferToWindow(self):
        return True

    #----------------------------------------------------------------------
    def TransferFromWindow(self):
        return True

    #----------------------------------------------------------------------
    def OnChar(self, event):
        keycode = int(event.GetKeyCode())
        if keycode < 256:
            if keycode > 96 & keycode < 123:
                keycode = keycode - 32
            #print keycode
            key = chr(keycode)
            #print key
            return
        event.Skip()

########################################################################
class ValidationDemo(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Text Validation Tutorial")

        panel = wx.Panel(self)
        textOne = wx.TextCtrl(panel, validator=CharValidator('no-alpha'))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(textOne, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = ValidationDemo()
    frame.Show()
    app.MainLoop()

2 个答案:

答案 0 :(得分:0)

您的演示帧与wxPython小部件树结构有一些问题。 这将允许您在文本控件中写入:

class ValidationDemo(wx.Frame):
    """"""
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Text Validation Tutorial")

        textOne = wx.TextCtrl(self, validator=CharValidator('no-alpha'))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(textOne, 1, wx.ALL| wx.EXPAND, 5)
        self.SetSizer(sizer)
        self.Fit()

在验证方面,您不会在计算后将信件发送到文本控件。更糟糕的是,你总是回来并杀死这个事件。

这有效(我在评论中解释):

def OnChar(self, event):
    keycode = int(event.GetKeyCode())
    #
    # get the widget where the text entry takes place
    object = self.GetWindow()
    #
    # for upper-case and for numbers, let the event go
    if (58 > keycode > 47) or ( 91 > keycode > 64):
        event.Skip()
    #
    # for lower-case, convert to upper and write it yourself
    elif 123 > keycode > 96:
        keycode = keycode - 32
        key = chr(keycode)
        object.write(key)

注意我为每个char事件调用self.GetWindow()。在您的情况下,只有一个控件使用验证器,您只能在验证器初始化时定义self.object一次。

答案 1 :(得分:0)

实际上所有混乱的验证功能都是不必要的。您只需要一个事件处理程序(请注意,它是草率的,并且硬编码到特定的textOne对象而不是输入对象。)

此外,移动InsertionPoint

似乎是一个很好的习惯
import wx

class ValidationDemo(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Text Validation Tutorial")

        panel = wx.Panel(self)
        self.textOne = wx.TextCtrl(panel)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.textOne, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

        self.Bind(wx.EVT_TEXT, self.OnTextType, self.textOne)

    def OnTextType(self, event):
        self.textOne.ChangeValue(unicode.upper(self.textOne.GetValue()))
        self.textOne.SetInsertionPointEnd()


# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = ValidationDemo()
    frame.Show()
    app.MainLoop()