]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTimeCtrl.py
1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o wx renamer needed for timectrl lib
8 # o presense of spin control causing probs (see spin ctrl demo for details)
10 # 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
12 # o New binders applied. Issues still exist.
16 import wx
.lib
.timectrl
as timectl
17 import wx
.lib
.scrolledpanel
as scrolled
19 #----------------------------------------------------------------------
21 class TestPanel( scrolled
.wxScrolledPanel
):
22 def __init__( self
, parent
, log
):
24 scrolled
.wxScrolledPanel
.__init
__( self
, parent
, -1 )
28 text1
= wx
.StaticText( self
, -1, "12-hour format:")
29 self
.time12
= timectl
.wxTimeCtrl( self
, -1, name
="12 hour control" )
30 spin1
= wx
.SpinButton( self
, -1, wx
.DefaultPosition
, (-1,20), 0 )
31 self
.time12
.BindSpinButton( spin1
)
33 text2
= wx
.StaticText( self
, -1, "24-hour format:")
34 spin2
= wx
.SpinButton( self
, -1, wx
.DefaultPosition
, (-1,20), 0 )
35 self
.time24
= timectl
.wxTimeCtrl(
36 self
, -1, name
="24 hour control", fmt24hr
=True,
40 text3
= wx
.StaticText( self
, -1, "No seconds\nor spin button:")
41 self
.spinless_ctrl
= timectl
.wxTimeCtrl(
42 self
, -1, name
="spinless control",
43 display_seconds
= False
46 grid
= wx
.FlexGridSizer( 0, 2, 10, 5 )
47 grid
.Add( text1
, 0, wx
.ALIGN_RIGHT
)
48 hbox1
= wx
.BoxSizer( wx
.HORIZONTAL
)
49 hbox1
.Add( self
.time12
, 0, wx
.ALIGN_CENTRE
)
50 hbox1
.Add( spin1
, 0, wx
.ALIGN_CENTRE
)
51 grid
.Add( hbox1
, 0, wx
.LEFT
)
53 grid
.Add( text2
, 0, wx
.ALIGN_RIGHT|wx
.TOP|wx
.BOTTOM
)
54 hbox2
= wx
.BoxSizer( wx
.HORIZONTAL
)
55 hbox2
.Add( self
.time24
, 0, wx
.ALIGN_CENTRE
)
56 hbox2
.Add( spin2
, 0, wx
.ALIGN_CENTRE
)
57 grid
.Add( hbox2
, 0, wx
.LEFT
)
59 grid
.Add( text3
, 0, wx
.ALIGN_RIGHT|wx
.TOP|wx
.BOTTOM
)
60 grid
.Add( self
.spinless_ctrl
, 0, wx
.LEFT
)
63 buttonChange
= wx
.Button( self
, -1, "Change Controls")
64 self
.radio12to24
= wx
.RadioButton(
65 self
, -1, "Copy 12-hour time to 24-hour control",
66 wx
.DefaultPosition
, wx
.DefaultSize
, wx
.RB_GROUP
69 self
.radio24to12
= wx
.RadioButton(
70 self
, -1, "Copy 24-hour time to 12-hour control"
73 self
.radioWx
= wx
.RadioButton( self
, -1, "Set controls to 'now' using wxDateTime")
74 self
.radioMx
= wx
.RadioButton( self
, -1, "Set controls to 'now' using mxDateTime")
76 radio_vbox
= wx
.BoxSizer( wx
.VERTICAL
)
77 radio_vbox
.Add( self
.radio12to24
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
78 radio_vbox
.Add( self
.radio24to12
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
79 radio_vbox
.Add( self
.radioWx
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
80 radio_vbox
.Add( self
.radioMx
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
82 box_label
= wx
.StaticBox( self
, -1, "Change Controls through API" )
83 buttonbox
= wx
.StaticBoxSizer( box_label
, wx
.HORIZONTAL
)
84 buttonbox
.Add( buttonChange
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 )
85 buttonbox
.Add( radio_vbox
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 )
87 hbox
= wx
.BoxSizer( wx
.HORIZONTAL
)
88 hbox
.Add( grid
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 15 )
89 hbox
.Add( buttonbox
, 0, wx
.ALIGN_RIGHT|wx
.BOTTOM
, 20 )
92 box_label
= wx
.StaticBox( self
, -1, "Bounds Control" )
93 boundsbox
= wx
.StaticBoxSizer( box_label
, wx
.HORIZONTAL
)
94 self
.set_bounds
= wx
.CheckBox( self
, -1, "Set time bounds:" )
96 minlabel
= wx
.StaticText( self
, -1, "minimum time:" )
97 self
.min = timectl
.wxTimeCtrl( self
, -1, name
="min", display_seconds
= False )
98 self
.min.Enable( False )
100 maxlabel
= wx
.StaticText( self
, -1, "maximum time:" )
101 self
.max = timectl
.wxTimeCtrl( self
, -1, name
="max", display_seconds
= False )
102 self
.max.Enable( False )
104 self
.limit_check
= wx
.CheckBox( self
, -1, "Limit control" )
106 label
= wx
.StaticText( self
, -1, "Resulting time control:" )
107 self
.target_ctrl
= timectl
.wxTimeCtrl( self
, -1, name
="new" )
109 grid2
= wx
.FlexGridSizer( 0, 2, 0, 0 )
110 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
111 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
113 grid2
.Add( self
.set_bounds
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
114 grid3
= wx
.FlexGridSizer( 0, 2, 5, 5 )
115 grid3
.Add(minlabel
, 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
116 grid3
.Add( self
.min, 0, wx
.ALIGN_LEFT
)
117 grid3
.Add(maxlabel
, 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
118 grid3
.Add( self
.max, 0, wx
.ALIGN_LEFT
)
119 grid2
.Add(grid3
, 0, wx
.ALIGN_LEFT
)
121 grid2
.Add( self
.limit_check
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
122 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
124 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
125 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
126 grid2
.Add( label
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
127 grid2
.Add( self
.target_ctrl
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
128 boundsbox
.Add(grid2
, 0, wx
.ALIGN_CENTER|wx
.EXPAND|wx
.ALL
, 5)
130 vbox
= wx
.BoxSizer( wx
.VERTICAL
)
132 vbox
.Add( hbox
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
133 vbox
.Add( boundsbox
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
136 outer_box
= wx
.BoxSizer( wx
.VERTICAL
)
137 outer_box
.Add( vbox
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
140 # Turn on mxDateTime option only if we can import the module:
142 from mx
import DateTime
144 self
.radioMx
.Enable( False )
147 self
.SetAutoLayout( True )
148 self
.SetSizer( outer_box
)
149 outer_box
.Fit( self
)
150 self
.SetupScrolling()
152 self
.Bind(wx
.EVT_BUTTON
, self
.OnButtonClick
, buttonChange
)
153 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.time12
)
154 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.time24
)
155 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.spinless_ctrl
)
156 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnBoundsCheck
, self
.set_bounds
)
157 self
.Bind(wx
.EVT_CHECKBOX
, self
.SetTargetMinMax
, self
.limit_check
)
158 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.SetTargetMinMax
, self
.min )
159 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.SetTargetMinMax
, self
.max )
160 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.target_ctrl
)
163 def OnTimeChange( self
, event
):
164 timectrl
= self
.FindWindowById( event
.GetId() )
165 ib_str
= [ " (out of bounds)", "" ]
167 self
.log
.write('%s time = %s%s\n' % ( timectrl
.GetName(), timectrl
.GetValue(), ib_str
[ timectrl
.IsInBounds() ] ) )
170 def OnButtonClick( self
, event
):
171 if self
.radio12to24
.GetValue():
172 self
.time24
.SetValue( self
.time12
.GetValue() )
174 elif self
.radio24to12
.GetValue():
175 self
.time12
.SetValue( self
.time24
.GetValue() )
177 elif self
.radioWx
.GetValue():
178 now
= wx
.DateTime_Now()
179 self
.time12
.SetValue( now
)
180 # (demonstrates that G/SetValue returns/takes a wxDateTime)
181 self
.time24
.SetValue( self
.time12
.GetValue(as_wxDateTime
=True) )
183 # (demonstrates that G/SetValue returns/takes a wxTimeSpan)
184 self
.spinless_ctrl
.SetValue( self
.time12
.GetValue(as_wxTimeSpan
=True) )
186 elif self
.radioMx
.GetValue():
187 from mx
import DateTime
189 self
.time12
.SetValue( now
)
191 # (demonstrates that G/SetValue returns/takes a DateTime)
192 self
.time24
.SetValue( self
.time12
.GetValue(as_mxDateTime
=True) )
194 # (demonstrates that G/SetValue returns/takes a DateTimeDelta)
195 self
.spinless_ctrl
.SetValue( self
.time12
.GetValue(as_mxDateTimeDelta
=True) )
198 def OnBoundsCheck( self
, event
):
199 self
.min.Enable( self
.set_bounds
.GetValue() )
200 self
.max.Enable( self
.set_bounds
.GetValue() )
201 self
.SetTargetMinMax()
204 def SetTargetMinMax( self
, event
=None ):
208 if self
.set_bounds
.GetValue():
209 min = self
.min.GetWxDateTime()
210 max = self
.max.GetWxDateTime()
212 min, max = None, None
214 cur_min
, cur_max
= self
.target_ctrl
.GetBounds()
216 # jmg - A little expirimental change to ensure that min
217 # or max contain valid values before we use them
218 if min and (min != cur_min
): self
.target_ctrl
.SetMin( min )
219 if max and (max != cur_max
): self
.target_ctrl
.SetMax( max )
221 self
.target_ctrl
.SetLimited( self
.limit_check
.GetValue() )
223 if min != cur_min
or max != cur_max
:
224 new_min
, new_max
= self
.target_ctrl
.GetBounds()
226 if new_min
and new_max
:
227 self
.log
.write( "current min, max: (%s, %s)\n" % ( new_min
.FormatTime(), new_max
.FormatTime() ) )
229 self
.log
.write( "current min, max: (None, None)\n" )
231 #----------------------------------------------------------------------
233 def runTest( frame
, nb
, log
):
234 win
= TestPanel( nb
, log
)
237 #----------------------------------------------------------------------
239 overview
= timectl
.__doc
__
241 if __name__
== '__main__':
244 run
.main(['', os
.path
.basename(sys
.argv
[0])])