Introduction
A control to facilitate capturing a date.
It combines a text control having user-definable input and display format, and a popup calendar that can be used as a date picker.
What Objects are Involved
The primary control is a wx.combo.ComboCtrl.
The underlying text control has bindings for EVT_SET_FOCUS and EVT_CHAR, which handle formatting and entering of dates.
It uses a customised popup, which invokes a wx.Dialog, which contains a wx.calendar.Calendar, together with Ok and Cancel buttons.
Process Overview
Add DateCtrl to your Panel in the normal way.
Parameters -
- size - size of underlying text control
- pos - position of underlying text control
- input_format - strftime[1] format to use for entering of date
- display_format - strftime[1] format to use for display of date after entry
title - used as title of wx.Dialog for calendar popup, and of wx.MessageDialog for errors
- default_to_today - boolean flag to initialise to today if currently blank
- allow_null - boolean flag to determine whether to allow date to remain blank
[1] strftime format - a format string containing directives governing how the date is to be formatted.
There are many directives - refer to the python manual for details.
Here are some common directives -
- %d = day of the month as a decimal number
- %m = month as a decimal number
- %y = year without century as a decimal number [00-99]
- %Y = year with century as a decimal number
- %a = locale's abbreviated weekday name
- %b = locale's abbreviated month name
Here are some typical formats, used to display 31st December 2010 -
- '%d/%m/%Y' = '31/12/2010'
- '%m.%d.%y' = '12.31.10'
- '%a %d %b %Y' = 'Fri 31 Dec 2010'
For input purposes, DateCtrl parses the format string, so that the user enters the date using only the numeric keypad. Delimiters and other literals are displayed, but automatically skipped over while entering.
Special Concerns
1. The calendar popup is displayed using CentreOnScreen(). Some may prefer to anchor it to the underlying text control, but this has not been implemented.
2. 'input_format' is invoked when the control gains focus. When the control loses focus, the date is re-displayed using 'display_format'. This is an effective way of allowing the user to confirm that the result matches his intentions. However, from a user-interface POV, it can be unsettling that it changes back to 'input_format' when it gains focus again. Feel free to add comments below to see if we can reach consensus on how this should be handled.
3. In order to evaluate the date entered, it is necessary to call the method convert_to_wx_date(). Strategies as to *when* this should be called may differ. In the attached program, it is called when the control after the date control gets focus.
4. There are a couple of minor issues regarding the appearance of the dropdown button. I spent some time investigating this but ended up making matters worse, so I left it.
Here is a link to a post from Robin discussing the underlying cause -
http://groups.google.co.za/group/wxpython-users/msg/fbd162ec435b7d91?hl=en
If anyone feels like following it up, patches will be very welcome.
Code Sample
Code is attached - datectrl.py
Revised version uploaded datectrl_1.3.py - added tooltip to dropdown button
Revised version uploaded datectrl_1.4.py - various bug fixes
Revised version uploaded datectrl_1.5.py - fixed bug in input format
Comments
Comments welcome. Frank Millman