[wxPython-users] SetMaxLength question

Frank Millman frank at chagford.com
Mon Sep 4 07:42:47 PDT 2006


Hugues JEAN-BAPTISTE wrote:
>
> Hi all,
> 
> For wx.TextCtrl, I use SetMaxLength to limit the number of 
> characters, the user can enter (The maximum length of 
> characters of the field in the database).
> Is there something equivalent for strings in a wx.Grid ?
> 

If you use a GridCellEditor, you can use exactly the same method.

I copied the GridCellEditor class from the demo into my program, and then
modified it like this -

class MyTextEditor(grd.PyGridCellEditor):
    def __init__(self,maxlen=0):  # [1]
        grd.PyGridCellEditor.__init__(self)
        self.maxlen = maxlen  # [1]

    def Create(self, parent, id, evtHandler=None):
        self._tc = wx.TextCtrl(parent, id, "",
            style=wx.TE_PROCESS_TAB|wx.TE_PROCESS_ENTER)
        self._tc.SetInsertionPoint(0)
        self.SetControl(self._tc)
        if self.maxlen:  # [2]
            self._tc.SetMaxLength(self.maxlen)  # [2]
        if evtHandler:
            self._tc.PushEventHandler(evtHandler)

[1] I created a new argument 'maxlen' and saved it as an attribute.
[2] I added these two lines.

HTH

Frank Millman





More information about the wxpython-users mailing list