| 1 | from wxPython.wx import * |
| 2 | from wxPython.lib.timectrl import * |
| 3 | |
| 4 | #---------------------------------------------------------------------- |
| 5 | |
| 6 | class TestPanel( wxPanel ): |
| 7 | def __init__( self, parent, log ): |
| 8 | |
| 9 | wxPanel.__init__( self, parent, -1 ) |
| 10 | self.log = log |
| 11 | panel = wxPanel( self, -1 ) |
| 12 | |
| 13 | grid = wxFlexGridSizer( 0, 2, 20, 0 ) |
| 14 | |
| 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 ) |
| 19 | |
| 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 ) |
| 25 | |
| 26 | |
| 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 ) |
| 31 | |
| 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 ) |
| 37 | |
| 38 | |
| 39 | text3 = wxStaticText( panel, 70, "A wxTimeCtrl without a spin button:") |
| 40 | self.spinless_ctrl = wxTimeCtrl( panel, 80, name="spinless control" ) |
| 41 | |
| 42 | grid.AddWindow( text3, 0, wxALIGN_RIGHT|wxTOP|wxBOTTOM, 5 ) |
| 43 | grid.AddWindow( self.spinless_ctrl, 0, wxLEFT, 5 ) |
| 44 | |
| 45 | |
| 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") |
| 51 | |
| 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 ) |
| 57 | |
| 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 ) |
| 62 | |
| 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 ) |
| 66 | |
| 67 | |
| 68 | # Turn on mxDateTime option only if we can import the module: |
| 69 | try: |
| 70 | from mx import DateTime |
| 71 | except ImportError: |
| 72 | self.radioMx.Enable( False ) |
| 73 | |
| 74 | |
| 75 | panel.SetAutoLayout( True ) |
| 76 | panel.SetSizer( outer_box ) |
| 77 | outer_box.Fit( panel ) |
| 78 | panel.Move( (50,50) ) |
| 79 | self.panel = panel |
| 80 | |
| 81 | |
| 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 ) |
| 85 | |
| 86 | EVT_BUTTON( self, buttonChange.GetId(), self.OnButtonClick ) |
| 87 | |
| 88 | |
| 89 | def OnTimeChange( self, event ): |
| 90 | timectrl = self.panel.FindWindowById( event.GetId() ) |
| 91 | self.log.write('%s time = %s\n' % ( timectrl.GetName(), timectrl.GetValue() ) ) |
| 92 | |
| 93 | def OnButtonClick( self, event ): |
| 94 | if self.radio12to24.GetValue(): |
| 95 | self.time24.SetValue( self.time12.GetValue() ) |
| 96 | |
| 97 | elif self.radio24to12.GetValue(): |
| 98 | self.time12.SetValue( self.time24.GetValue() ) |
| 99 | |
| 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 ) |
| 105 | |
| 106 | elif self.radioMx.GetValue(): |
| 107 | from mx import DateTime |
| 108 | now = DateTime.now() |
| 109 | self.time12.SetMxDateTime( now ) |
| 110 | self.time24.SetMxDateTime( now ) |
| 111 | self.spinless_ctrl.SetMxDateTime( now ) |
| 112 | |
| 113 | #---------------------------------------------------------------------- |
| 114 | |
| 115 | def runTest( frame, nb, log ): |
| 116 | win = TestPanel( nb, log ) |
| 117 | return win |
| 118 | |
| 119 | #---------------------------------------------------------------------- |
| 120 | |
| 121 | overview = """<html><body> |
| 122 | <P> |
| 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. |
| 126 | <P> |
| 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. |
| 130 | <P> |
| 131 | The <B>!</B> or <B>c</B> key sets the value of the control to <B><I>now.</I></B> |
| 132 | <P> |
| 133 | Here's the API for wxTimeCtrl: |
| 134 | <DL><PRE> |
| 135 | <B>wxTimeCtrl</B>( |
| 136 | parent, id = -1, |
| 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, |
| 143 | name = "time") |
| 144 | </PRE> |
| 145 | <UL> |
| 146 | <DT><B>value</B> |
| 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.) |
| 150 | <DL><B>size</B> |
| 151 | <DD>The size of the control will be automatically adjusted for 12/24 hour format |
| 152 | if wxDefaultSize is specified. |
| 153 | <BR> |
| 154 | <DT><B>fmt24hr</B> |
| 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. |
| 158 | <BR> |
| 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.) |
| 162 | <BR> |
| 163 | <DT><B>style</B> |
| 164 | <DD>By default, wxTimeCtrl will process TAB events, by allowing tab to the |
| 165 | different cells within the control. |
| 166 | </DL> |
| 167 | </UL> |
| 168 | <BR> |
| 169 | <BR> |
| 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 |
| 173 | <BR> |
| 174 | <DT><B>GetValue()</B> |
| 175 | <DD>Retrieves the string value of the time from the control |
| 176 | <BR> |
| 177 | <DT><B>SetWxDateTime(wxDateTime)</B> |
| 178 | <DD>Uses the time portion of a wxDateTime to construct a value for the control. |
| 179 | <BR> |
| 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 |
| 183 | "today".) |
| 184 | <BR> |
| 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 |
| 188 | is called. |
| 189 | <BR> |
| 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.) |
| 194 | <BR> |
| 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.) |
| 200 | <BR> |
| 201 | <DT><B>EVT_TIMEUPDATE(win, id, func)</B> |
| 202 | <DD>func is fired whenever the value of the control changes. |
| 203 | </DL> |
| 204 | </body></html> |
| 205 | """ |
| 206 | |
| 207 | |
| 208 | |
| 209 | if __name__ == '__main__': |
| 210 | import sys,os |
| 211 | import run |
| 212 | run.main(['', os.path.basename(sys.argv[0])]) |
| 213 | |