1 #! /usr/local/bin/python
2 #----------------------------------------------------------------------------
4 # Purpose: Calendar control display testing on panel
6 # Author: Lorne White (email: lwhite1@planet.eon.net)
9 # Version 0.5 1999/11/03
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
13 from wxPython
.wx
import *
14 from wxPython
.lib
.calendar
import wxCalendar
, Month
18 dir_path
= os
.getcwd()
21 # highlighted days in month
34 11: [6, 9, 12, 28, 29],
35 12: [8, 9, 10, 11, 20] }
37 # test of full window calendar control functions
44 monthlist
.append(name
)
47 class TestPanel(wxPanel
):
48 def __init__(self
, parent
, log
):
49 wxPanel
.__init
__(self
, parent
, -1)
53 self
.calend
= wxCalendar(self
, -1, wxPoint(100, 50), wxSize(200, 180))
58 # month list from DateTime module
60 monthlist
= GetMonthList()
62 self
.date
= wxComboBox(self
, 10, Month
[start_month
], wxPoint(100, 20), wxSize(90, -1), monthlist
, wxCB_DROPDOWN
)
63 EVT_COMBOBOX(self
, 10, self
.EvtComboBox
)
65 # set start month and year
67 self
.calend
.SetMonth(start_month
)
68 self
.calend
.SetYear(start_year
)
70 # set attributes of calendar
72 self
.calend
.HideTitle()
73 self
.calend
.HideGrid()
81 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
83 # scroll bar for month selection
85 self
.scroll
= wxScrollBar(self
, 40, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL
)
86 self
.scroll
.SetScrollbar(start_month
-1, 1, 12, 1, TRUE
)
87 EVT_COMMAND_SCROLL(self
, 40, self
.Scroll
)
89 # spin control for year selection
91 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(200, 20), wxSize(60, -1))
92 h
= self
.dtext
.GetSize().height
94 self
.spin
= wxSpinButton(self
, 20, wxPoint(270, 20), wxSize(h
*2, h
))
95 self
.spin
.SetRange(1980, 2010)
96 self
.spin
.SetValue(start_year
)
97 EVT_SPIN(self
, 20, self
.OnSpin
)
99 # button for calendar dialog test
101 wxStaticText(self
, -1, "Test Calendar Dialog", wxPoint(350, 50)).SetBackgroundColour(wxNamedColour('Red'))
103 bmp
= wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP
)
104 self
.but
= wxBitmapButton(self
, 60, bmp
, wxPoint(380, 80))#, wxSize(30, 30))
105 EVT_BUTTON(self
, 60, self
.TestDlg
)
107 # button for calendar window test
109 wxStaticText(self
, -1, "Test Calendar Window", wxPoint(350, 150)).SetBackgroundColour(wxNamedColour('Blue'))
111 bmp
= wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP
)
112 self
.but
= wxBitmapButton(self
, 160, bmp
, wxPoint(380, 180))#, wxSize(30, 30))
113 EVT_BUTTON(self
, 160, self
.TestFrame
)
117 def TestDlg(self
, event
):
118 dlg
= CalenDlg(self
, self
.log
)
123 # calendar window test
125 def TestFrame(self
, event
):
126 frame
= CalendFrame(self
, -1, "Test Calendar", self
.log
)
130 # month and year control events
132 def OnSpin(self
, event
):
133 year
= event
.GetPosition()
134 self
.dtext
.SetValue(str(year
))
135 self
.calend
.SetYear(year
)
136 self
.calend
.Refresh()
138 def EvtComboBox(self
, event
):
139 name
= event
.GetString()
140 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
141 monthval
= self
.date
.FindString(name
)
142 self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, TRUE
)
144 self
.calend
.SetMonth(monthval
+1)
147 def Scroll(self
, event
):
148 value
= self
.scroll
.GetThumbPosition()
149 monthval
= int(value
)+1
150 self
.calend
.SetMonth(monthval
)
152 self
.log
.WriteText('Month: %s\n' % value
)
154 name
= Month
[monthval
]
155 self
.date
.SetValue(name
)
159 def MouseClick(self
, evt
):
160 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
161 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
163 def OnCloseWindow(self
, event
):
166 # set the highlighted days for the calendar
168 def ResetDisplay(self
):
169 month
= self
.calend
.GetMonth()
171 set_days
= test_days
[month
]
173 set_days
= [1, 5, 12]
175 self
.calend
.SetSelDay(set_days
)
176 self
.calend
.Refresh()
178 # increment and decrement toolbar controls
180 def OnIncYear(self
, event
):
181 self
.calend
.IncYear()
184 def OnDecYear(self
, event
):
185 self
.calend
.DecYear()
188 def OnIncMonth(self
, event
):
189 self
.calend
.IncMonth()
192 def OnDecMonth(self
, event
):
193 self
.calend
.DecMonth()
196 def OnCurrent(self
, event
):
197 self
.calend
.SetCurrentDay()
200 # test the calendar control in a dialog
202 class CalenDlg(wxDialog
):
203 def __init__(self
, parent
, log
):
205 wxDialog
.__init
__(self
, parent
, -1, "Test Calendar", wxPyDefaultPosition
, wxSize(280, 300))
210 # get month list from DateTime
212 monthlist
= GetMonthList()
216 self
.date
= wxComboBox(self
, 100, Month
[start_month
], wxPoint(20, 20), wxSize(90, -1), monthlist
, wxCB_DROPDOWN
)
217 EVT_COMBOBOX(self
, 100, self
.EvtComboBox
)
219 # alternate spin button to control the month
221 h
= self
.date
.GetSize().height
222 self
.m_spin
= wxSpinButton(self
, 120, wxPoint(130, 20), wxSize(h
*2, h
), wxSP_VERTICAL
)
223 self
.m_spin
.SetRange(1, 12)
224 self
.m_spin
.SetValue(start_month
)
226 EVT_SPIN(self
, 120, self
.OnMonthSpin
)
228 # spin button to conrol the year
230 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(160, 20), wxSize(60, -1))
231 h
= self
.dtext
.GetSize().height
233 self
.y_spin
= wxSpinButton(self
, 20, wxPoint(220, 20), wxSize(h
*2, h
), wxSP_VERTICAL
)
234 self
.y_spin
.SetRange(1980, 2010)
235 self
.y_spin
.SetValue(start_year
)
237 EVT_SPIN(self
, 20, self
.OnYrSpin
)
239 # set the calendar and attributes
241 self
.calend
= wxCalendar(self
, -1, wxPoint(20, 60), wxSize(240, 200))
242 self
.calend
.SetMonth(start_month
)
243 self
.calend
.SetYear(start_year
)
245 self
.calend
.HideTitle()
248 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
250 # log the mouse clicks
252 def MouseClick(self
, evt
):
253 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
254 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
256 if evt
.click
== 'DLEFT':
257 self
.EndModal(wxID_OK
)
259 # month and year spin selection routines
261 def OnMonthSpin(self
, event
):
262 month
= event
.GetPosition()
263 self
.date
.SetValue(Month
[month
])
264 self
.calend
.SetMonth(month
)
265 self
.calend
.Refresh()
267 def OnYrSpin(self
, event
):
268 year
= event
.GetPosition()
269 self
.dtext
.SetValue(str(year
))
270 self
.calend
.SetYear(year
)
271 self
.calend
.Refresh()
273 def EvtComboBox(self
, event
):
274 name
= event
.GetString()
275 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
276 monthval
= self
.date
.FindString(name
)
277 self
.m_spin
.SetValue(monthval
+1)
279 self
.calend
.SetMonth(monthval
+1)
282 # set the calendar for highlighted days
284 def ResetDisplay(self
):
285 month
= self
.calend
.GetMonth()
287 set_days
= test_days
[month
]
289 set_days
= [1, 5, 12]
291 self
.calend
.SetSelDay(set_days
)
292 self
.calend
.Refresh()
294 # test of full window calendar control functions
296 class CalendFrame(wxFrame
):
297 def __init__(self
, parent
, id, title
, log
):
298 wxFrame
.__init
__(self
, parent
, id, title
, wxPyDefaultPosition
, wxSize(400, 400))
301 self
.CreateStatusBar()
302 self
.mainmenu
= wxMenuBar()
305 menu
= self
.MakeFileMenu()
306 self
.mainmenu
.Append(menu
, '&File')
308 self
.MakeToolMenu() # toolbar
310 self
.SetMenuBar(self
.mainmenu
)
311 self
.calend
= wxCalendar(self
, -1)
312 self
.calend
.SetCurrentDay()
313 self
.calend
.grid_color
= 'BLUE'
316 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
318 def MouseClick(self
, evt
):
319 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
320 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
322 def OnCloseWindow(self
, event
):
325 def ResetDisplay(self
):
326 month
= self
.calend
.GetMonth()
328 set_days
= test_days
[month
]
330 set_days
= [1, 5, 12]
332 self
.calend
.SetSelDay(set_days
)
333 self
.calend
.Refresh()
335 def OnIncYear(self
, event
):
336 self
.calend
.IncYear()
339 def OnDecYear(self
, event
):
340 self
.calend
.DecYear()
343 def OnIncMonth(self
, event
):
344 self
.calend
.IncMonth()
347 def OnDecMonth(self
, event
):
348 self
.calend
.DecMonth()
351 def OnCurrent(self
, event
):
352 self
.calend
.SetCurrentDay()
355 def MakeFileMenu(self
):
359 menu
.Append(mID
, 'Decrement', 'Next')
360 EVT_MENU(self
, mID
, self
.OnDecMonth
)
363 menu
.Append(mID
, 'Increment', 'Dec')
364 EVT_MENU(self
, mID
, self
.OnIncMonth
)
366 menu
.AppendSeparator()
369 menu
.Append(mID
, 'E&xit', 'Exit')
370 EVT_MENU(self
, mID
, self
.OnCloseWindow
)
374 def MakeToolMenu(self
):
375 tb
= self
.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER
)
377 bmp_path
= 'bitmaps/'
378 SetToolPath(self
, tb
, 10, bmp_path
+ 'DbDec.bmp', 'Dec Year')
379 EVT_TOOL(self
, 10, self
.OnDecYear
)
381 SetToolPath(self
, tb
, 15, bmp_path
+ 'Dec.bmp', 'Dec Month')
382 EVT_TOOL(self
, 15, self
.OnDecMonth
)
384 SetToolPath(self
, tb
, 30, bmp_path
+ 'Pt.bmp', 'Current Month')
385 EVT_TOOL(self
, 30, self
.OnCurrent
)
387 SetToolPath(self
, tb
, 40, bmp_path
+ 'Inc.bmp', 'Inc Month')
388 EVT_TOOL(self
, 40, self
.OnIncMonth
)
390 SetToolPath(self
, tb
, 45, bmp_path
+ 'DbInc.bmp', 'Inc Year')
391 EVT_TOOL(self
, 45, self
.OnIncYear
)
395 def SetToolPath(self
, tb
, id, bmp
, title
):
397 tb
.AddTool(id, wxBitmap(os
.path
.join(dir_path
, bmp
), wxBITMAP_TYPE_BMP
), wxNullBitmap
, false
, -1, -1, title
, title
)
402 frame
= CalendFrame(NULL
, -1, "Test Calendar")
404 self
.SetTopWindow(frame
)
407 #---------------------------------------------------------------------------
409 def MessageDlg(self
, message
, type = 'Message'):
410 dlg
= wxMessageDialog(self
, message
, type, wxOK | wxICON_INFORMATION
)
414 #---------------------------------------------------------------------------
421 if __name__
== '__main__':
425 #---------------------------------------------------------------------------
427 def runTest(frame
, nb
, log
):
428 win
= TestPanel(nb
, log
)
431 #---------------------------------------------------------------------------
435 This control provides a calendar control class for displaying and selecting dates.
437 See example for various methods used to set display month, year, and highlighted dates (different colour).