1 from wxPython
.wx
import *
2 from wxPython
.lib
.timectrl
import *
4 #----------------------------------------------------------------------
6 class TestPanel( wxPanel
):
7 def __init__( self
, parent
, log
):
9 wxPanel
.__init
__( self
, parent
, -1 )
11 panel
= wxPanel( self
, -1 )
13 grid
= wxFlexGridSizer( 0, 2, 20, 0 )
15 text1
= wxStaticText( panel
, 10, "A 12-hour format wxTimeCtrl:")
16 self
.time12
= wxTimeCtrl( panel
, 20, name
="12 hour control" )
17 spin1
= wxSpinButton( panel
, 30, wxDefaultPosition
, wxSize(-1,20), 0 )
18 self
.time12
.BindSpinButton( spin1
)
20 grid
.AddWindow( text1
, 0, wxALIGN_RIGHT
, 5 )
21 hbox1
= wxBoxSizer( wxHORIZONTAL
)
22 hbox1
.AddWindow( self
.time12
, 0, wxALIGN_CENTRE
, 5 )
23 hbox1
.AddWindow( spin1
, 0, wxALIGN_CENTRE
, 5 )
24 grid
.AddSizer( hbox1
, 0, wxLEFT
, 5 )
27 text2
= wxStaticText( panel
, 40, "A 24-hour format wxTimeCtrl:")
28 self
.time24
= wxTimeCtrl( panel
, 50, fmt24hr
=True, name
="24 hour control" )
29 spin2
= wxSpinButton( panel
, 60, wxDefaultPosition
, wxSize(-1,20), 0 )
30 self
.time24
.BindSpinButton( spin2
)
32 grid
.AddWindow( text2
, 0, wxALIGN_RIGHT|wxTOP|wxBOTTOM
, 5 )
33 hbox2
= wxBoxSizer( wxHORIZONTAL
)
34 hbox2
.AddWindow( self
.time24
, 0, wxALIGN_CENTRE
, 5 )
35 hbox2
.AddWindow( spin2
, 0, wxALIGN_CENTRE
, 5 )
36 grid
.AddSizer( hbox2
, 0, wxLEFT
, 5 )
39 text3
= wxStaticText( panel
, 70, "A wxTimeCtrl without a spin button:")
40 self
.spinless_ctrl
= wxTimeCtrl( panel
, 80, name
="spinless control" )
42 grid
.AddWindow( text3
, 0, wxALIGN_RIGHT|wxTOP|wxBOTTOM
, 5 )
43 grid
.AddWindow( self
.spinless_ctrl
, 0, wxLEFT
, 5 )
46 buttonChange
= wxButton( panel
, 100, "Change Controls")
47 self
.radio12to24
= wxRadioButton( panel
, 110, "Copy 12-hour time to 24-hour control", wxDefaultPosition
, wxDefaultSize
, wxRB_GROUP
)
48 self
.radio24to12
= wxRadioButton( panel
, 120, "Copy 24-hour time to 12-hour control")
49 self
.radioWx
= wxRadioButton( panel
, 130, "Set controls to 'now' using wxDateTime")
50 self
.radioMx
= wxRadioButton( panel
, 140, "Set controls to 'now' using mxDateTime")
52 radio_vbox
= wxBoxSizer( wxVERTICAL
)
53 radio_vbox
.AddWindow( self
.radio12to24
, 0, wxALIGN_CENTER_VERTICAL|wxALL
, 5 )
54 radio_vbox
.AddWindow( self
.radio24to12
, 0, wxALIGN_CENTER_VERTICAL|wxALL
, 5 )
55 radio_vbox
.AddWindow( self
.radioWx
, 0, wxALIGN_CENTER_VERTICAL|wxALL
, 5 )
56 radio_vbox
.AddWindow( self
.radioMx
, 0, wxALIGN_CENTER_VERTICAL|wxALL
, 5 )
58 box_label
= wxStaticBox( panel
, 90, "Change Controls through API" )
59 buttonbox
= wxStaticBoxSizer( box_label
, wxHORIZONTAL
)
60 buttonbox
.AddWindow( buttonChange
, 0, wxALIGN_CENTRE|wxALL
, 5 )
61 buttonbox
.AddSizer( radio_vbox
, 0, wxALIGN_CENTRE|wxALL
, 5 )
63 outer_box
= wxBoxSizer( wxVERTICAL
)
64 outer_box
.AddSizer( grid
, 0, wxALIGN_CENTRE|wxBOTTOM
, 20 )
65 outer_box
.AddSizer( buttonbox
, 0, wxALIGN_CENTRE|wxALL
, 5 )
68 # Turn on mxDateTime option only if we can import the module:
70 from mx
import DateTime
72 self
.radioMx
.Enable( False )
75 panel
.SetAutoLayout( True )
76 panel
.SetSizer( outer_box
)
77 outer_box
.Fit( panel
)
82 EVT_TIMEUPDATE( self
, self
.time12
.GetId(), self
.OnTimeChange
)
83 EVT_TIMEUPDATE( self
, self
.time24
.GetId(), self
.OnTimeChange
)
84 EVT_TIMEUPDATE( self
, self
.spinless_ctrl
.GetId(), self
.OnTimeChange
)
86 EVT_BUTTON( self
, buttonChange
.GetId(), self
.OnButtonClick
)
89 def OnTimeChange( self
, event
):
90 timectrl
= self
.panel
.FindWindowById( event
.GetId() )
91 self
.log
.write('%s time = %s\n' % ( timectrl
.GetName(), timectrl
.GetValue() ) )
93 def OnButtonClick( self
, event
):
94 if self
.radio12to24
.GetValue():
95 self
.time24
.SetValue( self
.time12
.GetValue() )
97 elif self
.radio24to12
.GetValue():
98 self
.time12
.SetValue( self
.time24
.GetValue() )
100 elif self
.radioWx
.GetValue():
101 now
= wxDateTime_Now()
102 self
.time12
.SetWxDateTime( now
)
103 self
.time24
.SetWxDateTime( now
)
104 self
.spinless_ctrl
.SetWxDateTime( now
)
106 elif self
.radioMx
.GetValue():
107 from mx
import DateTime
109 self
.time12
.SetMxDateTime( now
)
110 self
.time24
.SetMxDateTime( now
)
111 self
.spinless_ctrl
.SetMxDateTime( now
)
113 #----------------------------------------------------------------------
115 def runTest( frame
, nb
, log
):
116 win
= TestPanel( nb
, log
)
119 #----------------------------------------------------------------------
121 overview
= """<html><body>
123 <B>wxTimeCtrl</B> provides a multi-cell control that allows manipulation of a time
124 value. It supports 12 or 24 hour format, and you can use wxDateTime or mxDateTime
125 to get/set values from the control.
127 Left/right/tab keys to switch cells within a wxTimeCtrl, and the up/down arrows act
128 like a spin control. wxTimeCtrl also allows for an actual spin button to be attached
129 to the control, so that it acts like the up/down arrow keys.
131 The <B>!</B> or <B>c</B> key sets the value of the control to <B><I>now.</I></B>
133 Here's the API for wxTimeCtrl:
137 <B>value</B> = '12:00:00 AM',
138 pos = wxDefaultPosition,
139 size = wxDefaultSize,
140 <B>fmt24hr</B> = False,
141 <B>spinButton</B> = None,
142 <B>style</B> = wxTE_PROCESS_TAB,
147 <DD>If no initial value is set, the default will be midnight; if an illegal string
148 is specified, a ValueError will result. (You can always later set the initial time
149 with SetValue() after instantiation of the control.)
151 <DD>The size of the control will be automatically adjusted for 12/24 hour format
152 if wxDefaultSize is specified.
155 <DD>If True, control will display time in 24 hour time format; if False, it will
156 use 12 hour AM/PM format. SetValue() will adjust values accordingly for the
157 control, based on the format specified.
159 <DT><B>spinButton</B>
160 <DD>If specified, this button's events will be bound to the behavior of the
161 wxTimeCtrl, working like up/down cursor key events. (See BindSpinButton.)
164 <DD>By default, wxTimeCtrl will process TAB events, by allowing tab to the
165 different cells within the control.
170 <DT><B>SetValue(time_string)</B>
171 <DD>Sets the value of the control to a particular time, given a valid time string;
172 raises ValueError on invalid value
174 <DT><B>GetValue()</B>
175 <DD>Retrieves the string value of the time from the control
177 <DT><B>SetWxDateTime(wxDateTime)</B>
178 <DD>Uses the time portion of a wxDateTime to construct a value for the control.
180 <DT><B>GetWxDateTime()</B>
181 <DD>Retrieves the value of the control, and applies it to the wxDateTimeFromHMS()
182 constructor, and returns the resulting value. (This returns the date portion as
185 <DT><B>SetMxDateTime(mxDateTime)</B>
186 <DD>Uses the time portion of an mxDateTime to construct a value for the control.
187 <EM>NOTE:</EM> This imports mx.DateTime at runtime only if this or the Get function
190 <DT><B>GetMxDateTime()</B>
191 <DD>Retrieves the value of the control and applies it to the DateTime.Time()
192 constructor, and returns the resulting value. (mxDateTime is smart enough to
193 know this is just a time value.)
195 <DT><B>BindSpinButton(wxSpinBtton)</B>
196 <DD>Binds an externally created spin button to the control, so that up/down spin
197 events change the active cell or selection in the control (in addition to the
198 up/down cursor keys.) (This is primarily to allow you to create a "standard"
199 interface to time controls, as seen in Windows.)
201 <DT><B>EVT_TIMEUPDATE(win, id, func)</B>
202 <DD>func is fired whenever the value of the control changes.
209 if __name__
== '__main__':
212 run
.main(['', os
.path
.basename(sys
.argv
[0])])