pythoncalendarwxpythoncalendar-control

Setting the Date on the wx.calendar CalendarCtrl Widget


I'm trying to set the selected date so that it is always the current day. How would I go about this?

Currently if I let the widget sit for more than a day, the selected date doesn't update. Thus the date would be behind. How would I go about changing the selected date, so that it's always up to date?


Solution

  • you could set a timer (wx.Timer) instance to produce a wx.EVT_TIMER every several few seconds/minutes and bind the event to a method in charge of updating the calendar with the current date (wx.DateTime_Now()) if required.

    Here you have a minimal working demo code (try to change the date: it will go back to the current date after a second):

    import  wx
    import  wx.calendar
    
    class MyCalendar(wx.Frame):
        def __init__(self, *args, **kargs):
            wx.Frame.__init__(self, *args, **kargs)
            self.cal = wx.calendar.CalendarCtrl(self, -1, wx.DateTime_Now())
            self.timer = wx.Timer(self)
            self.timer.Start(1000)
            self.Bind(wx.EVT_TIMER, self.update_date)
    
        def update_date(self, evt):
            date = wx.DateTime_Now()
            self.cal.SetDate(date)    
    
    if __name__ == '__main__':  
        app = wx.PySimpleApp()
        frame = MyCalendar(None)
        frame.Show()
        app.MainLoop()