When using a wx.DateTimePickerCtrl, I would like to programmatically change its value and have it refreshed in my dialog.
To change the value, I can legitimate use SetValue() with a wx.DateTime object, which seems to work. However, the dialog box doesn’t refresh.
I suspect it has to cope with events… It is said in the documentation that EVT_DATE_CHANGED is not triggered when using the SetValue() method of a DateTimePickerCtrl. Should this event be generated? How? or is it some kind of “repaint” event that need to be sent?
Moreover, I get the following exception when issuing a GetValue() behind:
wx._core.PyAssertionError: C++ assertion "m_date.IsValid() == dt.IsValid()
&& (!dt.IsValid() || dt == m_date)" failed at
..\..\src\msw\datectrl.cpp(274) in wxDatePickerCtrl::GetValue(): bug in
wxDatePickerCtrl: m_date not in sync
It was opened by a wxPython user back in november 2006 and fixed by Robin.
If you can upgrade to the latest wxPython version, I believe it is
fixed. I am using wx.DatePickerCtrl all over my GUIs and I have never
seen those problems. My suggestion is to install 2.8.4, and don't miss
the new sexy demo
Well, upgrading to 2.8.4.0 didn’t help much, the issue remains (the only
difference is in the line number of the assertion).
wx._core.PyAssertionError: C++ assertion “m_date.IsValid() == dt.IsValid()
&& (!dt.IsValid() || dt == m_date)” failed at
…..\src\msw\datectrl.cpp(278) in wxDatePickerCtrl::GetValue(): bug in
wxDatePickerCtrl: m_date not in sync
Your date:
date = wx.DateTimeFromDMY ( 1, 12, 7)
Is an invalid one, even if IsValid() returns True. 12 should not be
there, as for some strange reason the enumeration of months in
wx.DatePickerCtrl starts at 0 (January) and ends at 11 (December).
Moreover, you are asking for a date 7 years after Christ was born :-D.
I believe the underlying C++ is limited to a narrower range of dates,
but I may be wrong. Maybe you wanted the 2007 year? Makes much more
sense to me.
Try the attached scripts and see if it does what you want.
Andrea.
“Imagination Is The Only Weapon In The War Against Reality.”
IMHO, you would expect that the wx.DateTimeFromDMY ( 1, 12, 7) would raise an exception for invalid input data.
Invalid dates are allowed, one of the reasons being that it makes it possible to have unentered or inititialized values. In Python we would normally do that by using None, but in C++ wx usually uses an instance of the target class in an invalid state. You can test validity of a wx.DateTime object by calling it's IsOk() method, or just testing the date object itself:
>>> d = wx.DateTimeFromDMY(99, 99, 99)
>>> d
<wx.DateTime: "INVALID" at _28b95908_p_wxDateTime>
>>> d.IsOk()
False
>>> if d:
... print 'yes'
... else:
... print 'no'
...
no
Another wart on this is that 12 is the value of wx.DateTime.Inv_Month, which is used as the default value of the month parameter in some places, and usually means to use the current month. For example:
>>> d = wx.DateTimeFromDMY(1, 12, 7)
>>> d
<wx.DateTime: "Tue May 1 00:00:00 0007" at _98586608_p_wxDateTime>
>>> d = wx.DateTimeFromDMY(1, wx.DateTime.Dec, 7)
>>> d
<wx.DateTime: "Sat Dec 1 00:00:00 0007" at _b8397908_p_wxDateTime>
>>> d = wx.DateTimeFromDMY(day=1, year=7)
>>> d
<wx.DateTime: "Tue May 1 00:00:00 0007" at _285a6e08_p_wxDateTime>
>>> d = wx.DateTimeFromDMY(14, wx.DateTime.Inv_Month, wx.DateTime.Inv_Year)
>>> d
<wx.DateTime: "Mon May 14 00:00:00 2007" at _a0ac7208_p_wxDateTime>
>>>
So the moral of the story is that when dealing with months in wx.DateTime you should always use the month constants defined in the class: