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
, size
=wxSize(400, 400),
233 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
234 EVT_CLOSE(self
, self
.OnCloseWindow
)
237 self
.CreateStatusBar()
238 self
.mainmenu
= wxMenuBar()
241 menu
= self
.MakeFileMenu()
242 self
.mainmenu
.Append(menu
, '&File')
244 self
.MakeToolMenu() # toolbar
246 self
.SetMenuBar(self
.mainmenu
)
247 self
.calend
= wxCalendar(self
, -1)
248 self
.calend
.SetCurrentDay()
249 self
.calend
.grid_color
= 'BLUE'
250 self
.calend
.SetBusType()
251 # self.calend.ShowWeekEnd()
255 self
.Connect(self
.calend
.GetId(), -1, 2100, self
.MouseClick
)
257 def MouseClick(self
, evt
):
258 text
= '%s CLICK %02d/%02d/%d' % (evt
.click
, evt
.day
, evt
.month
, evt
.year
) # format date
259 self
.log
.WriteText('Date Selected: ' + text
+ '\n')
261 def OnCloseWindow(self
, event
):
264 def ResetDisplay(self
):
265 month
= self
.calend
.GetMonth()
267 set_days
= test_days
[month
]
269 set_days
= [1, 5, 12]
271 self
.calend
.AddSelect([2, 16], 'GREEN', 'WHITE')
273 self
.calend
.SetSelDay(set_days
)
274 self
.calend
.Refresh()
276 def OnIncYear(self
, event
):
277 self
.calend
.IncYear()
280 def OnDecYear(self
, event
):
281 self
.calend
.DecYear()
284 def OnIncMonth(self
, event
):
285 self
.calend
.IncMonth()
288 def OnDecMonth(self
, event
):
289 self
.calend
.DecMonth()
292 def OnCurrent(self
, event
):
293 self
.calend
.SetCurrentDay()
296 def MakeFileMenu(self
):
300 menu
.Append(mID
, 'Decrement', 'Next')
301 EVT_MENU(self
, mID
, self
.OnDecMonth
)
304 menu
.Append(mID
, 'Increment', 'Dec')
305 EVT_MENU(self
, mID
, self
.OnIncMonth
)
307 menu
.AppendSeparator()
310 menu
.Append(mID
, 'E&xit', 'Exit')
311 EVT_MENU(self
, mID
, self
.OnCloseWindow
)
315 def MakeToolMenu(self
):
316 tb
= self
.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER
)
319 SetToolPath(self
, tb
, mID
, images
.getDbDecBitmap(), 'Dec Year')
320 EVT_TOOL(self
, mID
, self
.OnDecYear
)
323 SetToolPath(self
, tb
, mID
, images
.getDecBitmap(), 'Dec Month')
324 EVT_TOOL(self
, mID
, self
.OnDecMonth
)
327 SetToolPath(self
, tb
, mID
, images
.getPtBitmap(), 'Current Month')
328 EVT_TOOL(self
, mID
, self
.OnCurrent
)
331 SetToolPath(self
, tb
, mID
, images
.getIncBitmap(), 'Inc Month')
332 EVT_TOOL(self
, mID
, self
.OnIncMonth
)
335 SetToolPath(self
, tb
, mID
, images
.getDbIncBitmap(), 'Inc Year')
336 EVT_TOOL(self
, mID
, self
.OnIncYear
)
340 #---------------------------------------------------------------------------
342 # example class for printing/previewing calendars
345 def __init__(self
, parent
, month
, year
):
352 self
.printData
= wxPrintData()
355 self
.grid_color
= 'BLUE'
356 self
.back_color
= 'WHITE'
357 self
.sel_color
= 'RED'
358 self
.high_color
= 'LIGHT BLUE'
362 self
.sel_key
= None # last used by
363 self
.sel_lst
= [] # highlighted selected days
366 self
.hide_title
= FALSE
367 self
.hide_grid
= FALSE
388 def SetDates(self
, month
, year
):
392 def SetStyleDef(self
, desc
):
395 def SetCopies(self
, copies
): # number of copies of label
398 def SetStart(self
, start
): # start position of label
402 printout
= SetPrintout(self
)
403 printout2
= SetPrintout(self
)
404 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
405 if not self
.preview
.Ok():
406 wxMessageBox("There was a problem printing!", "Printing", wxOK
)
409 self
.preview
.SetZoom(60) # initial zoom value
411 frame
= wxPreviewFrame(self
.preview
, self
.frame
, "Print preview")
414 frame
.SetPosition(self
.frame
.GetPosition())
415 frame
.SetSize(self
.frame
.GetSize())
419 pdd
= wxPrintDialogData()
420 pdd
.SetPrintData(self
.printData
)
421 printer
= wxPrinter(pdd
)
422 printout
= SetPrintout(self
)
423 frame
= wxFrame(None, -1, "Test")
424 if not printer
.Print(frame
, printout
):
425 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
427 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
430 def DoDrawing(self
, DC
):
431 size
= DC
.GetSizeTuple()
434 cal
= PrtCalDraw(self
)
436 if self
.preview
is None:
437 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
438 cal
.SetPreview(FALSE
)
441 if self
.preview
== 1:
442 cal
.SetPSize(size
[0]/self
.pagew
, size
[1]/self
.pageh
)
444 cal
.SetPSize(self
.pwidth
, self
.pheight
)
446 cal
.SetPreview(self
.preview
)
448 cal
.hide_title
= self
.hide_title
# set the calendar parameters
449 cal
.hide_grid
= self
.hide_grid
451 cal
.grid_color
= self
.grid_color
452 cal
.high_color
= self
.high_color
453 cal
.back_color
= self
.back_color
454 cal
.outer_border
= FALSE
458 cal_size
= wxSize(3.0, 3.0)
459 cal
.SetSize(cal_size
)
461 year
, month
= self
.year
, self
.month
467 cal
.SetCal(year
, month
) # current month
471 set_days
= test_days
[month
]
473 set_days
= [1, 5, 12]
475 cal
.AddSelect([2, 16], 'GREEN', 'WHITE')
477 cal
.DrawCal(DC
, set_days
)
479 year
, month
= self
.IncMonth(year
, month
)
481 x
= x
+ 4.0 # next colum
485 self
.ymax
= DC
.MaxY()
486 self
.xmax
= DC
.MaxX()
488 def IncMonth(self
, year
, month
): # next month
496 def GetTotalPages(self
):
500 def SetPage(self
, page
):
503 def SetPageSize(self
, width
, height
):
504 self
.pwidth
, self
.pheight
= width
, height
506 def SetTotalSize(self
, width
, height
):
507 self
.ptwidth
, self
.ptheight
= width
, height
509 def SetPreview(self
, preview
, scale
):
510 self
.preview
= preview
513 def SetTotalSize(self
, width
, height
):
515 self
.ptheight
= height
517 def SetToolPath(self
, tb
, id, bmp
, title
):
518 tb
.AddSimpleTool(id, bmp
, title
, title
)
520 class SetPrintout(wxPrintout
):
521 def __init__(self
, canvas
):
522 wxPrintout
.__init
__(self
)
526 def OnBeginDocument(self
, start
, end
):
527 return self
.base_OnBeginDocument(start
, end
)
529 def OnEndDocument(self
):
530 self
.base_OnEndDocument()
532 def HasPage(self
, page
):
533 if page
<= self
.end_pg
:
538 def GetPageInfo(self
):
539 self
.end_pg
= self
.canvas
.GetTotalPages()
545 return (str_pg
, end_pg
, str_pg
, end_pg
)
547 def OnPreparePrinting(self
):
548 self
.base_OnPreparePrinting()
550 def OnBeginPrinting(self
):
553 self
.preview
= self
.IsPreview()
555 self
.pixelsPerInch
= self
.GetPPIScreen()
557 self
.pixelsPerInch
= self
.GetPPIPrinter()
559 (w
, h
) = dc
.GetSizeTuple()
560 scaleX
= float(w
) / 1000
561 scaleY
= float(h
) / 1000
562 self
.printUserScale
= min(scaleX
, scaleY
)
564 self
.base_OnBeginPrinting()
567 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
568 return self
.psizew
, self
.psizeh
570 def GetTotalSize(self
):
571 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
572 return self
.ptsizew
, self
.ptsizeh
574 def OnPrintPage(self
, page
):
576 (w
, h
) = dc
.GetSizeTuple()
577 scaleX
= float(w
) / 1000
578 scaleY
= float(h
) / 1000
579 self
.printUserScale
= min(scaleX
, scaleY
)
580 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
582 self
.preview
= self
.IsPreview()
584 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
585 self
.canvas
.SetPage(page
)
587 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
588 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
590 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
591 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
593 self
.canvas
.DoDrawing(dc
)
598 frame
= CalendFrame(None, -1, "Test Calendar")
600 self
.SetTopWindow(frame
)
603 #---------------------------------------------------------------------------
605 def MessageDlg(self
, message
, type = 'Message'):
606 dlg
= wxMessageDialog(self
, message
, type, wxOK | wxICON_INFORMATION
)
610 #---------------------------------------------------------------------------
617 if __name__
== '__main__':
621 #---------------------------------------------------------------------------
623 def runTest(frame
, nb
, log
):
624 win
= TestPanel(nb
, log
, frame
)
627 #---------------------------------------------------------------------------
631 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.
633 Additional features include weekend highlighting and business type Monday-Sunday format.
635 See example for various methods used to set display month, year, and highlighted dates (different font and background colours).