Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to limit TextCtrl to accept numbers only in wxPython?

I want to have a text control that only accepts numbers. (Just integer values like 45 or 366)

What is the best way to do this?

like image 374
Jeroen Dirks Avatar asked Sep 02 '09 17:09

Jeroen Dirks


1 Answers

I had to do something similar, checking for alphanumeric codes. The tip on EVT_CHAR was the right thing:

class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        self.entry = wx.TextCtrl(self, -1)
        self.entry.Bind(wx.EVT_CHAR, self.handle_keypress)

    def handle_keypress(self, event):
        keycode = event.GetKeyCode()
        if keycode < 255:
            # valid ASCII
            if chr(keycode).isalnum():
                # Valid alphanumeric character
                event.Skip()
like image 183
Jay P. Avatar answered Oct 16 '22 16:10

Jay P.