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,
displaySeconds=False,
fmt24hr=False,
validator=wx.DefaultValidator,
style=wx.TE_PROCESS_TAB)
self.time_ctrl.SetMin("06:00:00")
self.time_ctrl.SetMax("12:30:00")
self.time_ctrl.SetLimited(True)
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)
self.SetSizer(sizer)
self.Layout()
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()
frame.Show(True)
return True
if __name__ == "__main__":
app = App(0)
app.MainLoop()
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