I would like my wx.TextCtrl box to only accept numbers, preferably to only 2 decimal places. Where should i start?

should i use an input mask or are there styles too control this kind of input?

value = wx.TextCtrl(parent)

Edited 6 Years Ago by Archenemie: n/a

You can use something like this ...

# exploring the wxPython widget
# wx.TextCtrl(parent, id, value, pos, size, style)
# limit entry to numeric values with max 2 decimals

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("white")
        s = "Enter the price:"
        label = wx.StaticText(self, -1, s, pos=(10,10))
        self.edit = wx.TextCtrl(self, -1, pos=(10, 30))
        # respond to enter key when focus is on edit
        self.edit.Bind(wx.EVT_TEXT_ENTER, self.onAction)

    def onAction(self, event):
        """
        check for numeric entry and limit to 2 decimals
        accepted result is in self.value
        """
        raw_value = self.edit.GetValue().strip()
        # numeric check
        if all(x in '0123456789.+-' for x in raw_value):
            # convert to float and limit to 2 decimals
            self.value = round(float(raw_value), 2)
            self.edit.ChangeValue(str(self.value))
        else:
            self.edit.ChangeValue("Number only")


app = wx.App(0)
# create a MyFrame instance and show the frame
mytitle = 'Numeric entry test'
width = 400
height = 300
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()

i'm new in using python..
ay i know what is "wx"? because with the python im using 2.7.1.. wx cant find when i used to run your program...

i'm new in using python..
ay i know what is "wx"? because with the python im using 2.7.1.. wx cant find when i used to run your program...

wxPython is a library binding python programs to the c++ wxWidgets GUI toolkit. See this site http://www.wxpython.org/. wx is the name of the python module to import when you have wxPython installed.

To be complete, Linux requires style=wx.TE_PROCESS_ENTER

import wx
 
class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("white")
        s = "Enter the price:"
        label = wx.StaticText(self, -1, s, pos=(10,10),)
        self.edit = wx.TextCtrl(self, -1, pos=(10, 30), \
                                size=(100, 25), style=wx.TE_PROCESS_ENTER)
        # respond to enter key when focus is on edit
        self.edit.Bind(wx.EVT_TEXT_ENTER, self.onAction)
        self.edit.SetFocus()
         
    def onAction(self, event):
        """
        check for numeric entry and limit to 2 decimals
        accepted result is in self.value
        """
        raw_value = self.edit.GetValue().strip()
        print raw_value
        # numeric check
        if all(x in '0123456789.+-' for x in raw_value):
            # convert to float and limit to 2 decimals
            self.value = round(float(raw_value), 2)
            self.edit.ChangeValue("%9.2f" % (self.value))
        else:
            self.edit.ChangeValue("Number only")
 
 
app = wx.App(0)
# create a MyFrame instance and show the frame
mytitle = 'Numeric entry test'
width = 450
height = 200
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()

An easier way of doing this is as follows

import wx
from wx.lib.masked import NumCtrl

class MyFrame(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Quick Silver', size = (180, 100))

        panel = wx.Panel(self)

        input = wx.lib.masked.NumCtrl(panel, pos = (20,20), fractionWidth = 2) # for 2 decimal places.

app = wx.App(redirect = False)
frame = MyFrame(parent = None, id = -1)
frame.Show()
app.MainLoop()

Although number control doesn't always behave "comfortably".

Edited 5 Years Ago by Archenemie: n/a

This question has already been answered. Start a new discussion instead.