PyValidator and floats, howto?

Hi friends!

I have trying understand this *damn* PyValidator and don't know how I can check text while input, so only float number will be accepted... Why there Validator method that do not do anything?

Inside OnChar method I can only check if inputed *char* is valid, but don't know how to validate entire string.

Suppose float value should correspond some pattern, i.e. only two digints after dot. How to implement float validator based on PyValidator class?

Thanks!

validator.py (838 Bytes)

Hi Basil,

I don't want to insist, but why don't you used just the wx.lib.masked.numctrl or wx.lib.masked.textctrl with an appropriate mask?

Werner

Basil Shubin wrote:

···

Hi friends!

I have trying understand this *damn* PyValidator and don't know how I can check text while input, so only float number will be accepted... Why there Validator method that do not do anything?

Inside OnChar method I can only check if inputed *char* is valid, but don't know how to validate entire string.

Suppose float value should correspond some pattern, i.e. only two digints after dot. How to implement float validator based on PyValidator class?

Thanks!
------------------------------------------------------------------------

# -*- coding: utf-8 -*-

import wx
import re

class floatValidator(wx.PyValidator):
            def __init__(self, pyVar=None):
        wx.PyValidator.__init__(self)
        self.numList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.']
        self.Bind(wx.EVT_CHAR, self.OnChar)

    def Clone(self):
        return floatValidator()

    def Validate(self, win):
        pass

    def OnChar(self, event): key = event.GetKeyCode()

        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
            event.Skip()
            return

        if chr(key) in self.numList:
            event.Skip()
            return

        if not wx.Validator_IsSilent():
            wx.Bell()

        # Returning without calling even.Skip eats the event before it
        # gets to the text control
        return
  ------------------------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Werner F. Bruhin wrote:

Hi Basil,

I don't want to insist, but why don't you used just the wx.lib.masked.numctrl or wx.lib.masked.textctrl with an appropriate mask?

Thank for suggestion, but I don't like very much masked controls behavior. It's looks very good for phone number, but for floats... First, is dot sign - I have it! Second, is that I can move input cursor forward and back, while field is empty.

Hi Basil,

Basil Shubin wrote:

Werner F. Bruhin wrote:

Hi Basil,

I don't want to insist, but why don't you used just the wx.lib.masked.numctrl or wx.lib.masked.textctrl with an appropriate mask?

Thank for suggestion, but I don't like very much masked controls behavior. It's looks very good for phone number, but for floats... First, is dot sign - I have it!

I am not sure I understand, do you want a ',' instead of '.'? If yes, look at DecimalChar and GroupChar property.

Second, is that I can move input cursor forward and back, while field is empty.

I don't know if this can be changed. I most set SelectOnEntry to True, so when you move into the control you can then type the value.

Werner

Hi Werner,

Werner F. Bruhin wrote:

Hi Basil,

Basil Shubin wrote:

Werner F. Bruhin wrote:

Hi Basil,

I don't want to insist, but why don't you used just the wx.lib.masked.numctrl or wx.lib.masked.textctrl with an appropriate mask?

Thank for suggestion, but I don't like very much masked controls behavior. It's looks very good for phone number, but for floats... First, is dot sign - I have it!

I am not sure I understand, do you want a ',' instead of '.'? If yes, look at DecimalChar and GroupChar property.

The sign itself, no matter if it's a ',' or '.' The problem is, that user must have way to input fraction part of float, or just enter only integer. But if he input '.', than he can only input two digits after that.

Second, is that I can move input cursor forward and back, while field is empty.

I don't know if this can be changed. I most set SelectOnEntry to True, so when you move into the control you can then type the value.

Alas, my question stays unanswered, but if there is no solution for PyValidator, the masked control is my last chance.

Hi Basil,

Basil Shubin wrote:

Hi Werner,

Werner F. Bruhin wrote:

Hi Basil,

Basil Shubin wrote:

Werner F. Bruhin wrote:

Hi Basil,

I don't want to insist, but why don't you used just the wx.lib.masked.numctrl or wx.lib.masked.textctrl with an appropriate mask?

Thank for suggestion, but I don't like very much masked controls behavior. It's looks very good for phone number, but for floats... First, is dot sign - I have it!

I am not sure I understand, do you want a ',' instead of '.'? If yes, look at DecimalChar and GroupChar property.

The sign itself, no matter if it's a ',' or '.' The problem is, that user must have way to input fraction part of float, or just enter only integer. But if he input '.', than he can only input two digits after that.

One can enter as many digits after the '.' as the control is defined for, just did it with 5 digits, one can enter fewer then defined by the control but not more.

Second, is that I can move input cursor forward and back, while field is empty.

I don't know if this can be changed. I most set SelectOnEntry to True, so when you move into the control you can then type the value.

Alas, my question stays unanswered, but if there is no solution for PyValidator, the masked control is my last chance.

Maybe you find your answer with the masked controls, as they do validation. I assume you know about the wxPython validator demo under Core Windows/Controls.

Maybe posting a small runnable sample will get some people to review and suggest changes.

Werner

Basil Shubin wrote:

Hi friends!

I have trying understand this *damn* PyValidator and don't know how I can check text while input, so only float number will be accepted... Why there Validator method that do not do anything?

Inside OnChar method I can only check if inputed *char* is valid, but don't know how to validate entire string.

In the validator methods you can get the window it is attached to with self.GetWindow(), then you can get the current value with window.GetValue() or whatever the specific window class uses.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Basil Shubin wrote:

Hi friends!

I have trying understand this *damn* PyValidator and don't know how I can check text while input, so only float number will be accepted... Why there Validator method that do not do anything?

Inside OnChar method I can only check if inputed *char* is valid, but don't know how to validate entire string.

In the validator methods you can get the window it is attached to with self.GetWindow(), then you can get the current value with window.GetValue() or whatever the specific window class uses.

Doh, but what was a main point? Input validation was occurs in OnChar method, but what is role of Validator method? I can put into it any garbage and it will be work. Problem is that I can't realise how to use it and where?

Thanks!

Hi Werner and other,

Werner F. Bruhin wrote:

Alas, my question stays unanswered, but if there is no solution for PyValidator, the masked control is my last chance.

Maybe you find your answer with the masked controls, as they do validation. I assume you know about the wxPython validator demo under Core Windows/Controls.

Maybe posting a small runnable sample will get some people to review and suggest changes.

Here is test app. Main problem that I can enter as many '.' signs as I want :-), but in ideal this should not happens.

But what is purpose of Validator method after all?

Thanks!

testapp.zip (2.1 KB)

Hi Basil,

Basil Shubin wrote:

Hi Werner and other,

Werner F. Bruhin wrote:

Alas, my question stays unanswered, but if there is no solution for PyValidator, the masked control is my last chance.

Maybe you find your answer with the masked controls, as they do validation. I assume you know about the wxPython validator demo under Core Windows/Controls.

Maybe posting a small runnable sample will get some people to review and suggest changes.

Here is test app. Main problem that I can enter as many '.' signs as I want :-), but in ideal this should not happens.

But what is purpose of Validator method after all?

You just can not insult it, otherwise it will not do it's job :wink: .

I removed the '.' from numList and
I changed:
        if not wx.Validator_IsSilent():
to:
        if not self.Validate(self):

Then you need to put some code into Validate to allow the first '.' but no more and put up some error message.

I don't know why wx.Validator_IsSilent() does not do the trick, maybe Robin can explain this.

Werner

Werner F. Bruhin wrote:

Hi Basil,

Basil Shubin wrote:

Hi Werner and other,

Werner F. Bruhin wrote:

Alas, my question stays unanswered, but if there is no solution for PyValidator, the masked control is my last chance.

Maybe you find your answer with the masked controls, as they do validation. I assume you know about the wxPython validator demo under Core Windows/Controls.

Maybe posting a small runnable sample will get some people to review and suggest changes.

Here is test app. Main problem that I can enter as many '.' signs as I want :-), but in ideal this should not happens.

But what is purpose of Validator method after all?

You just can not insult it, otherwise it will not do it's job :wink: .

I've shocked it and it surrender to me! :wink:

I removed the '.' from numList and
I changed:
       if not wx.Validator_IsSilent():
to:
       if not self.Validate(self):

Then you need to put some code into Validate to allow the first '.' but no more and put up some error message.

Hah, here is my solution :stuck_out_tongue: (see attachment)! What you can say about it? Is there any other more elegant solution?

There is still one more issue: how to limit amount of digits after dot?

Thanks!

validator.py (998 Bytes)

Basil Shubin wrote:

But what is purpose of Validator method after all?

If the control is on a dialog then the Validate method is called automatically when the Ok button is pressed. If any of the validators on the dialog return false from their Validate method then the Ok button will not succeed and the dialog will not be dismissed. If you are not using a dialog then to be able to use the validators' Validate method you need to call the parent window's Validate method at some point and check the return value yourself.

Please see the demo and the book for more details and examples.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!