Min and max params from TimeCtrl

Good morning. I am having some problems to use the Widget TimeCtrl and is that I don’t know exactly in what format should be the min and max parameters. What I am looking for is that the field has as minimum the time of 06:00 and as maximum the 12:30. I have tried passing 6000 for example for the minimum but nothing, the field can go from 00:00 to 23:00, I leave the code of the widget in case it helps, thanks in advance.

time_control = TimeCtrl(parent, -1, displaySeconds=False, fmt24hr=False, validator=wx.DefaultValidator, style=wx.TE_PROCESS_TAB, limited=True, min=6000)

The first thing I noticed was that you were only passing the min value in your example. The documentation says you must pass both the min and max values, otherwise they will be ignored.

However, that does not fix the problem, because the TimeCtrl code appears to be ignoring the values passed to __init__() in the min, max and limited parameters. This may be due to the instance attribute controlInitialized not being set at the point that the SetParameters() method is called. Perhaps that is a bug?

An alternative that did work for me was to call SetMin(), SetMax() and SetLimited() after the TimeCtrl has been created.

import wx
from wx.lib.masked import TimeCtrl

class TimeFrame(wx.Frame):
    """Create a frame with a TimeCtrl. """
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'TimeCtrl', wx.DefaultPosition, (200, 100))

        self.time_ctrl = TimeCtrl(self, -1,


        self.button = wx.Button(self, wx.ID_ANY, "Get Value")
        self.Bind(wx.EVT_BUTTON, self.OnGetVal, self.button)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.time_ctrl, 0, 0, 0)
        sizer.Add(self.button, 0, 0, 0)

    def OnGetVal(self, _event):
        value = self.time_ctrl.GetValue()
        is_valid = self.time_ctrl.IsValid(value)
        print(value, is_valid)

class App(wx.App):
    def OnInit(self):
        frame = TimeFrame()
        return True

if __name__ == "__main__":

    app = App(0)

The docstrings for SetMin() and SetMax() say that their parameters should be integer or None. However, the value is passed to the GetWxDateTime() method which accepts:

    * time string
    * :class:`DateTime`
    * :class:`TimeSpan`
    * mxDateTime
    * mxDateTimeDelta

In my example I used strings for the min and max values.

Tested using Python 3.8.10 + wxPython 4.1.1 gtk3 (phoenix) wxWidgets 3.1.5 + Linux Mint 20.2

1 Like

I see, thank you very much, I had also tried with SetMin() and SetMax() but separately, apparently I didn’t read in the documentation where it said that both should go, and with SetLimited() clearly, it is solved :slight_smile:

It seems like those 3 arguments are always overridden on purpose: https://github.com/wxWidgets/Phoenix/blob/64e5d863f7833f10df6a0fbcf3221a730562224b/wx/lib/masked/timectrl.py#L411 but no actual reason is given in the comment. Maybe they realized that something bad can happen if the arguments are honored, or maybe it’s just an overlook. Unfortunately, the code of this “masked” package is a little hairy… maybe it’s worth investigating, but in the meantime, setting the properties at a later time will do the trick.

1 Like

Hi, Posts

Apart from the module documentation @RichardT mentioned, there is also that of __init__ . Seems inconsistent EDIT The latter doesn’t mention about **kwargs.