1 #----------------------------------------------------------------------------
3 # Purpose: Calendar control display testing on panel for wxPython demo
5 # Author: Lorne White (email: lwhite1@planet.eon.net)
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
12 from wxPython
.wx
import *
13 from wxPython
.lib
.calendar
import wxCalendar
, Month
, PrtCalDraw
, CalenDlg
19 # highlighted days in month
32 11: [6, 9, 12, 28, 29],
33 12: [8, 9, 10, 11, 20] }
35 # test of full window calendar control functions
42 monthlist
.append(name
)
45 class TestPanel(wxPanel
):
46 def __init__(self
, parent
, log
, frame
):
47 wxPanel
.__init
__(self
, parent
, -1)
52 self
.calend
= wxCalendar(self
, -1, wxPoint(100, 50), wxSize(200, 180))
54 # start_month = 2 # preselect the date for calendar
57 start_month
= self
.calend
.GetMonth() # get the current month & year
58 start_year
= self
.calend
.GetYear()
60 # month list from DateTime module
62 monthlist
= GetMonthList()
65 self
.date
= wxComboBox(self
, mID
, Month
[start_month
], wxPoint(100, 20), wxSize(90, -1), monthlist
, wxCB_DROPDOWN
)
66 EVT_COMBOBOX(self
, mID
, self
.EvtComboBox
)
68 # set start month and year
70 self
.calend
.SetMonth(start_month
)
71 self
.calend
.SetYear(start_year
)
73 # set attributes of calendar
75 self
.calend
.hide_title
= TRUE
76 self
.calend
.HideGrid()
77 self
.calend
.SetWeekColor('WHITE', 'BLACK')
85 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
87 # scroll bar for month selection
90 self
.scroll
= wxScrollBar(self
, mID
, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL
)
91 self
.scroll
.SetScrollbar(start_month
-1, 1, 12, 1, TRUE
)
92 EVT_COMMAND_SCROLL(self
, mID
, self
.Scroll
)
94 # spin control for year selection
96 self
.dtext
= wxTextCtrl(self
, -1, str(start_year
), wxPoint(200, 20), wxSize(60, -1))
97 h
= self
.dtext
.GetSize().height
100 self
.spin
= wxSpinButton(self
, mID
, wxPoint(270, 20), wxSize(h
*2, h
))
101 self
.spin
.SetRange(1980, 2010)
102 self
.spin
.SetValue(start_year
)
103 EVT_SPIN(self
, mID
, self
.OnSpin
)
105 # button for calendar dialog test
107 wxStaticText(self
, -1, "Test Calendar Dialog", wxPoint(350, 50), wxSize(150, -1))
110 bmp
= images
.getCalendarBitmap()
111 self
.but
= wxBitmapButton(self
, mID
, bmp
, wxPoint(380, 80))#, wxSize(30, 30))
112 EVT_BUTTON(self
, mID
, self
.TestDlg
)
114 # button for calendar window test
116 wxStaticText(self
, -1, "Test Calendar Window", wxPoint(350, 150), wxSize(150, -1))
119 self
.but
= wxBitmapButton(self
, mID
, bmp
, wxPoint(380, 180))#, wxSize(30, 30))
120 EVT_BUTTON(self
, mID
, self
.TestFrame
)
122 wxStaticText(self
, -1, "Test Calendar Print", wxPoint(350, 250), wxSize(150, -1))
125 self
.but
= wxBitmapButton(self
, mID
, bmp
, wxPoint(380, 280))#, wxSize(30, 30))
126 EVT_BUTTON(self
, mID
, self
.OnPreview
)
130 def TestDlg(self
, event
): # test the date dialog
133 if dlg
.ShowModal() == wxID_OK
:
138 new_date
= str(month
) + '/'+ str(day
) + '/'+ str(year
)
139 self
.log
.WriteText('Date Selected: %s\n' % new_date
)
141 self
.log
.WriteText('No Date Selected')
143 # calendar window test
145 def TestFrame(self
, event
):
146 frame
= CalendFrame(self
, -1, "Test Calendar", self
.log
)
150 # calendar print preview
152 def OnPreview(self
, event
):
153 month
= self
.calend
.GetMonth()
154 year
= self
.calend
.GetYear()
156 prt
= PrintCalend(self
.frame
, month
, year
)
159 # month and year control events
161 def OnSpin(self
, event
):
162 year
= event
.GetPosition()
163 self
.dtext
.SetValue(str(year
))
164 self
.calend
.SetYear(year
)
165 self
.calend
.Refresh()
167 def EvtComboBox(self
, event
):
168 name
= event
.GetString()
169 self
.log
.WriteText('EvtComboBox: %s\n' % name
)
170 monthval
= self
.date
.FindString(name
)
171 self
.scroll
.SetScrollbar(monthval
, 1, 12, 1, TRUE
)
173 self
.calend
.SetMonth(monthval
+1)
176 def Scroll(self
, event
):
177 value
= self
.scroll
.GetThumbPosition()
178 monthval
= int(value
)+1
179 self
.calend
.SetMonth(monthval
)
181 self
.log
.WriteText('Month: %s\n' % value
)
183 name
= Month
[monthval
]
184 self
.date
.SetValue(name
)
188 def MouseClick(self
, evt
):
189 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
190 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
193 # set the highlighted days for the calendar
195 def ResetDisplay(self
):
196 month
= self
.calend
.GetMonth()
198 set_days
= test_days
[month
]
200 set_days
= [1, 5, 12]
202 self
.calend
.AddSelect([4, 11], 'BLUE', 'WHITE')
203 self
.calend
.SetSelDay(set_days
)
204 self
.calend
.Refresh()
206 # increment and decrement toolbar controls
208 def OnIncYear(self
, event
):
209 self
.calend
.IncYear()
212 def OnDecYear(self
, event
):
213 self
.calend
.DecYear()
216 def OnIncMonth(self
, event
):
217 self
.calend
.IncMonth()
220 def OnDecMonth(self
, event
):
221 self
.calend
.DecMonth()
224 def OnCurrent(self
, event
):
225 self
.calend
.SetCurrentDay()
228 # test of full window calendar control functions
230 class CalendFrame(wxFrame
):
231 def __init__(self
, parent
, id, title
, log
):
232 wxFrame
.__init
__(self
, parent
, id, title
, wxPyDefaultPosition
, wxSize(400, 400))
233 EVT_CLOSE(self
, self
.OnCloseWindow
)
236 self
.CreateStatusBar()
237 self
.mainmenu
= wxMenuBar()
240 menu
= self
.MakeFileMenu()
241 self
.mainmenu
.Append(menu
, '&File')
243 self
.MakeToolMenu() # toolbar
245 self
.SetMenuBar(self
.mainmenu
)
246 self
.calend
= wxCalendar(self
, -1)
247 self
.calend
.SetCurrentDay()
248 self
.calend
.grid_color
= 'BLUE'
249 self
.calend
.SetBusType()
250 # self.calend.ShowWeekEnd()
254 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
256 def MouseClick(self
, evt
):
257 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
258 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
260 def OnCloseWindow(self
, event
):
263 def ResetDisplay(self
):
264 month
= self
.calend
.GetMonth()
266 set_days
= test_days
[month
]
268 set_days
= [1, 5, 12]
270 self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE')
272 self
.calend
.SetSelDay(set_days
)
273 self
.calend
.Refresh()
275 def OnIncYear(self
, event
):
276 self
.calend
.IncYear()
279 def OnDecYear(self
, event
):
280 self
.calend
.DecYear()
283 def OnIncMonth(self
, event
):
284 self
.calend
.IncMonth()
287 def OnDecMonth(self
, event
):
288 self
.calend
.DecMonth()
291 def OnCurrent(self
, event
):
292 self
.calend
.SetCurrentDay()
295 def MakeFileMenu(self
):
299 menu
.Append(mID
, 'Decrement', 'Next')
300 EVT_MENU(self
, mID
, self
.OnDecMonth
)
303 menu
.Append(mID
, 'Increment', 'Dec')
304 EVT_MENU(self
, mID
, self
.OnIncMonth
)
306 menu
.AppendSeparator()
309 menu
.Append(mID
, 'E&xit', 'Exit')
310 EVT_MENU(self
, mID
, self
.OnCloseWindow
)
314 def MakeToolMenu(self
):
315 tb
= self
.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER
)
318 SetToolPath(self
, tb
, mID
, images
.getDbDecBitmap(), 'Dec Year')
319 EVT_TOOL(self
, mID
, self
.OnDecYear
)
322 SetToolPath(self
, tb
, mID
, images
.getDecBitmap(), 'Dec Month')
323 EVT_TOOL(self
, mID
, self
.OnDecMonth
)
326 SetToolPath(self
, tb
, mID
, images
.getPtBitmap(), 'Current Month')
327 EVT_TOOL(self
, mID
, self
.OnCurrent
)
330 SetToolPath(self
, tb
, mID
, images
.getIncBitmap(), 'Inc Month')
331 EVT_TOOL(self
, mID
, self
.OnIncMonth
)
334 SetToolPath(self
, tb
, mID
, images
.getDbIncBitmap(), 'Inc Year')
335 EVT_TOOL(self
, mID
, self
.OnIncYear
)
339 #---------------------------------------------------------------------------
341 # example class for printing/previewing calendars
344 def __init__(self
, parent
, month
, year
):
351 self
.printData
= wxPrintData()
354 self
.grid_color
= 'BLUE'
355 self
.back_color
= 'WHITE'
356 self
.sel_color
= 'RED'
357 self
.high_color
= 'LIGHT BLUE'
361 self
.sel_key
= None # last used by
362 self
.sel_lst
= [] # highlighted selected days
365 self
.hide_title
= FALSE
366 self
.hide_grid
= FALSE
387 def SetDates(self
, month
, year
):
391 def SetStyleDef(self
, desc
):
394 def SetCopies(self
, copies
): # number of copies of label
397 def SetStart(self
, start
): # start position of label
401 printout
= SetPrintout(self
)
402 printout2
= SetPrintout(self
)
403 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
404 if not self
.preview
.Ok():
405 wxMessageBox("There was a problem printing!", "Printing", wxOK
)
408 self
.preview
.SetZoom(60) # initial zoom value
410 frame
= wxPreviewFrame(self
.preview
, self
.frame
, "Print preview")
413 frame
.SetPosition(self
.frame
.GetPosition())
414 frame
.SetSize(self
.frame
.GetSize())
418 pdd
= wxPrintDialogData()
419 pdd
.SetPrintData(self
.printData
)
420 printer
= wxPrinter(pdd
)
421 printout
= SetPrintout(self
)
422 frame
= wxFrame(NULL
, -1, "Test")
423 if not printer
.Print(frame
, printout
):
424 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
426 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
429 def DoDrawing(self
, DC
):
430 size
= DC
.GetSizeTuple()
433 cal
= PrtCalDraw(self
)
435 if self
.preview
is None:
436 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
437 cal
.SetPreview(FALSE
)
440 if self
.preview
== 1:
441 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
443 cal
.SetPSize(self
.pwidth
, self
.pheight
)
445 cal
.SetPreview(self
.preview
)
447 cal
.hide_title
= self
.hide_title
# set the calendar parameters
448 cal
.hide_grid
= self
.hide_grid
450 cal
.grid_color
= self
.grid_color
451 cal
.high_color
= self
.high_color
452 cal
.back_color
= self
.back_color
453 cal
.outer_border
= FALSE
457 cal_size
= wxSize(3.0, 3.0)
458 cal
.SetSize(cal_size
)
460 year
, month
= self
.year
, self
.month
466 cal
.SetCal(year
, month
) # current month
470 set_days
= test_days
[month
]
472 set_days
= [1, 5, 12]
474 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
476 cal
.DrawCal(DC
, set_days
)
478 year
, month
= self
.IncMonth(year
, month
)
480 x
= x
+ 4.0 # next colum
484 self
.ymax
= DC
.MaxY()
485 self
.xmax
= DC
.MaxX()
487 def IncMonth(self
, year
, month
): # next month
495 def GetTotalPages(self
):
499 def SetPage(self
, page
):
502 def SetPageSize(self
, width
, height
):
503 self
.pwidth
, self
.pheight
= width
, height
505 def SetTotalSize(self
, width
, height
):
506 self
.ptwidth
, self
.ptheight
= width
, height
508 def SetPreview(self
, preview
, scale
):
509 self
.preview
= preview
512 def SetTotalSize(self
, width
, height
):
514 self
.ptheight
= height
516 def SetToolPath(self
, tb
, id, bmp
, title
):
517 tb
.AddSimpleTool(id, bmp
, title
, title
)
519 class SetPrintout(wxPrintout
):
520 def __init__(self
, canvas
):
521 wxPrintout
.__init
__(self
)
525 def OnBeginDocument(self
, start
, end
):
526 return self
.base_OnBeginDocument(start
, end
)
528 def OnEndDocument(self
):
529 self
.base_OnEndDocument()
531 def HasPage(self
, page
):
532 if page
<= self
.end_pg
:
537 def GetPageInfo(self
):
538 self
.end_pg
= self
.canvas
.GetTotalPages()
544 return (str_pg
, end_pg
, str_pg
, end_pg
)
546 def OnPreparePrinting(self
):
547 self
.base_OnPreparePrinting()
549 def OnBeginPrinting(self
):
552 self
.preview
= self
.IsPreview()
554 self
.pixelsPerInch
= self
.GetPPIScreen()
556 self
.pixelsPerInch
= self
.GetPPIPrinter()
558 (w
, h
) = dc
.GetSizeTuple()
559 scaleX
= float(w
) / 1000
560 scaleY
= float(h
) / 1000
561 self
.printUserScale
= min(scaleX
, scaleY
)
563 self
.base_OnBeginPrinting()
566 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
567 return self
.psizew
, self
.psizeh
569 def GetTotalSize(self
):
570 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
571 return self
.ptsizew
, self
.ptsizeh
573 def OnPrintPage(self
, page
):
575 (w
, h
) = dc
.GetSizeTuple()
576 scaleX
= float(w
) / 1000
577 scaleY
= float(h
) / 1000
578 self
.printUserScale
= min(scaleX
, scaleY
)
579 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
581 self
.preview
= self
.IsPreview()
583 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
584 self
.canvas
.SetPage(page
)
586 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
587 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
589 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
590 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
592 self
.canvas
.DoDrawing(dc
)
597 frame
= CalendFrame(NULL
, -1, "Test Calendar")
599 self
.SetTopWindow(frame
)
602 #---------------------------------------------------------------------------
604 def MessageDlg(self
, message
, type = 'Message'):
605 dlg
= wxMessageDialog(self
, message
, type, wxOK | wxICON_INFORMATION
)
609 #---------------------------------------------------------------------------
616 if __name__
== '__main__':
620 #---------------------------------------------------------------------------
622 def runTest(frame
, nb
, log
):
623 win
= TestPanel(nb
, log
, frame
)
626 #---------------------------------------------------------------------------
630 This control provides a calendar control class for displaying and selecting dates. In addition, the class is extended and can now be used for printing/previewing.
632 Additional features include weekend highlighting and business type Monday-Sunday format.
634 See example for various methods used to set display month, year, and highlighted dates (different font and background colours).