Numeric Keyboard Layout

4 views
Skip to first unread message

Rich E

unread,
Aug 17, 2010, 9:49:58 AM8/17/10
to pymt...@googlegroups.com
Hi,

I found the NumericKeybardLayout() in the example script ui_widgets_composed_vkeyboard_numerical.py and it is really nice for a numbox widget I am making.  A couple questions now: 

Any reason it isn't included in vkeyboard.py?  I did see one thing I had to change, the big comma is only useful in making number strings, but I would think if you wanted that then you could just insert it into the label automatically with every three digits (or is this a European versus American thing? :)  I changed it to a decimal point in order to make floating point values.

The thing is that I would like to only allow number characters and discard all other input.  I tried doing this in my child class:

    def on_text_change(self, text):
        ''' Make sure text is only a valid number
            and assign it to our value.
        '''
        try:
            val = float(text)
        except ValueError:
            print('value error:', text)
            if text == '':
                self.value = 0
                self.label = str(0)
            else:
                self.label = self.value
        else:
            self.value = val


But the non-number characters are still staying in there.  Any suggestions on how to limit the text to numbers, and possibly - or .?

cheers,
Rich

Mathieu Virbel

unread,
Aug 17, 2010, 10:18:10 AM8/17/10
to pymt...@googlegroups.com
It's just not yet included, an issue is already open for that :
http://code.google.com/p/pymt/issues/detail?id=256

It should be available as another layout like AZERTY / QUERTY / US
NUMERIC / FR NUMERIC...

And if you want to remove all other characters, you could do:
re.sub('[^0-9\.\-]*', '', your_text_var).

It's not enough to be really usable and cover all cases, but it's a start :)

Mathieu

2010/8/17 Rich E <reaki...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "pymt-dev" group.
> To post to this group, send email to pymt...@googlegroups.com.
> To unsubscribe from this group, send email to
> pymt-dev+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pymt-dev?hl=en.
>

Rich E

unread,
Aug 18, 2010, 3:39:32 AM8/18/10
to pymt...@googlegroups.com
Thanks for the help,

On Wed, Aug 18, 2010 at 12:18 AM, Mathieu Virbel <txp...@gmail.com> wrote:
It's just not yet included, an issue is already open for that :
http://code.google.com/p/pymt/issues/detail?id=256

It should be available as another layout like AZERTY / QUERTY / US
NUMERIC / FR NUMERIC...

I put my star in :)  I suppose it is getting taken care of, as the example I found was only just added.
 
And if you want to remove all other characters, you could do:
re.sub('[^0-9\.\-]*', '', your_text_var).

Hm.. that does the trick, the same as try: float(var) along with a catch, and is probably more bug proof (and I still need to convert the result to a float). But, my problem is that I can't overwrite the label in MTTextInput (I have subclassed it in my MTNumBox class) when I find a non-digit character.  So, the next time on_text_change() gets fired, the old character is still in the handed text variable, once again needing to be removed.  This creates a problem with the backspace and arrow (not implemented yet) functionality.  

I tried just overwriting MTTextInput's label (I am a little confused about .label versus .value for this class) but it is storing the string somewhere else, then giving it back to me with non-digit characters again.  Do you know how I can overwrite this?

Rich

Mathieu Virbel

unread,
Aug 18, 2010, 12:08:20 PM8/18/10
to pymt...@googlegroups.com
This should do the trick. However, a nicer fix is required for inclusion :)

---
from pymt import *
import re

m = MTTextInput()
m.old_value = ''
@m.event
def on_text_change(value):
try:
if value != '':
float(value)
m.old_value = value
except ValueError:
m.keyboard.text = m.value = m.old_value

runTouchApp(m)
---

2010/8/18 Rich E <rich....@gmail.com>:

Reply all
Reply to author
Forward
0 new messages