【问题标题】:Validating data in wxPython在 wxPython 中验证数据
【发布时间】:2013-11-03 22:42:41
【问题描述】:

我正在尝试为表单上的输入创建验证器。我已经了解到,在 wxPython 中,由于缺乏对标准 wxTextValidator 等的支持,因此必须从 wx.Validator 继承。

我的问题是:

  • 如何有效地检查字符串是否符合简单规则(请不要使用正则表达式)

    acceptableChars = ['a', 'b', ...]

    all(char in acceptableChars for char in string)

    这样的东西有效率吗?以及如何干净地指定所有字母数字或数字?或者是否有任何现成的类或函数?

  • 将覆盖 Validate 方法仅在输入数据时保留约束 - 我的意思是它会阻止用户将数字输入字母数字 TextCtrl 还是仅在关闭模式对话框时检查?

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    Validate()默认只在对话框即将关闭时调用,但你也可以在控件失去焦点时自己调用。最后,如果你的控件根本不接受某些字符,你也可以拦截wxEVT_CHAR事件,防止它们被输入。我相信 wxPython 演示展示了如何做到这一点。

    【讨论】:

      【解决方案2】:
      "12345".isdigit() # True
      "123.45".isdigit() # False
      "abcde".isalpha() # True
      "abcde1".isalpha() # False
      "abcde12345".isalnum() # True
      "!!??".isalnum() # False
      

      对于其他情况,您必须使用您的代码

      acceptableChars = "ab5-?" # or acceptableChars = ['a', 'b', '5', '-', '?']
      
      all(char in acceptableChars for char in string)
      

      .

      def isValid(string, acceptableChars):
          return all(char in acceptableChars for char in string)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-04
        • 2011-10-29
        • 2011-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-15
        相关资源
        最近更新 更多