Hi,
Does anyone have any idea why SpinButton won’t work in the code below when down button is pressed when time value is 00:00? I expect it to be set to 23:55 when I press the down button, but it doesn’t work.
Also, the up button won’t work either, when the time value reaches 03:45. Strange.
import wx
from wx.lib.embeddedimage import PyEmbeddedImage
from wx.lib.masked import TimeCtrl
from datetime import datetime, timedelta
class MyTimeCtrl(TimeCtrl):
def __init__(self, the_parent, time_val):
TimeCtrl.__init__(self, parent=the_parent, id=-1, value=time_val, fmt24hr=True)
self.SetParameters(display_seconds=False)
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
date_val = datetime(2023, 10, 1)
self.pnl = wx.Panel(self)
spin = wx.SpinButton(self.pnl, -1, wx.DefaultPosition, (-1,20), wx.SP_VERTICAL, name="spin")
spin.Bind(wx.EVT_SPIN_UP, self.on_time_up)
spin.Bind(wx.EVT_SPIN_DOWN, self.on_time_down)
self.time_ctrl = MyTimeCtrl(self.pnl, time_val=date_val.strftime("%H:%M"))
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.time_ctrl, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
sizer.Add(spin, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.pnl.SetSizer(sizer)
def on_time_up(self, event):
the_val = self.time_ctrl.GetValue()
hoursxx = int(the_val[:2])
minutesxx = int(the_val[3:])
st_val = datetime(2023, 5, 10, hoursxx, minutesxx) + timedelta(minutes=5)
self.time_ctrl.SetValue(st_val.strftime("%H:%M"))
self.time_ctrl.Refresh()
def on_time_down(self, event):
the_val = self.time_ctrl.GetValue()
hoursxx = int(the_val[:2])
minutesxx = int(the_val[3:])
st_val = datetime(2023, 5, 10, hoursxx, minutesxx) - timedelta(minutes=5)
self.time_ctrl.SetValue(st_val.strftime("%H:%M"))
self.time_ctrl.Refresh()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()