📄 calendar.py
字号:
#----------------------------------------------------------------------------
# Name: Calendar.py
# Purpose: Calendar control display testing on panel for wxPython demo
#
# Author: Lorne White (email: lwhite1@planet.eon.net)
#
# Version 0.9
# Date: Feb 26, 2001
# Licence: wxWindows license
#----------------------------------------------------------------------------
# 11/15/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o Updated for wx namespace
#
# 11/26/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o Ugh. AFter updating to the Bind() method, things lock up
# on various control clicks. Will have to debug. Only seems
# to happen on windows with calendar controls, though.
#
# 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o Lockup issue clarification: it appears that the spinner is
# the culprit.
#
import os
import wx
import wx.lib.calendar
import images
# highlighted days in month
test_days ={ 0: [],
1: [3, 7, 9, 21],
2: [2, 10, 4, 9],
3: [4, 20, 29],
4: [1, 12, 22],
5: [2, 10, 15],
6: [4, 8, 17],
7: [6, 7, 8],
8: [5, 10, 20],
9: [1, 2, 5, 29],
10: [2, 4, 6, 22],
11: [6, 9, 12, 28, 29],
12: [8, 9, 10, 11, 20] }
# test of full window calendar control functions
def GetMonthList():
monthlist = []
for i in range(13):
name = wx.lib.calendar.Month[i]
if name != None:
monthlist.append(name)
return monthlist
class TestPanel(wx.Panel):
def __init__(self, parent, log, frame):
wx.Panel.__init__(self, parent, -1)
self.log = log
self.frame = frame
self.calend = wx.lib.calendar.Calendar(self, -1, (100, 50), (200, 180))
# start_month = 2 # preselect the date for calendar
# start_year = 2001
start_month = self.calend.GetMonth() # get the current month & year
start_year = self.calend.GetYear()
# month list from DateTime module
monthlist = GetMonthList()
self.date = wx.ComboBox(self, -1, "",
(100, 20), (90, -1),
monthlist, wx.CB_DROPDOWN)
self.date.SetSelection(start_month-1)
self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)
# set start month and year
self.calend.SetMonth(start_month)
self.calend.SetYear(start_year)
# set attributes of calendar
self.calend.hide_title = True
self.calend.HideGrid()
self.calend.SetWeekColor('WHITE', 'BLACK')
# display routine
self.ResetDisplay()
# mouse click event
self.Bind(wx.lib.calendar.EVT_CALENDAR, self.MouseClick, self.calend)
# scroll bar for month selection
self.scroll = wx.ScrollBar(self, -1, (100, 240), (200, 20), wx.SB_HORIZONTAL)
self.scroll.SetScrollbar(start_month-1, 1, 12, 1, True)
self.Bind(wx.EVT_COMMAND_SCROLL, self.Scroll, self.scroll)
# spin control for year selection
self.dtext = wx.TextCtrl(self, -1, str(start_year), (200, 20), (60, -1))
h = self.dtext.GetSize().height
self.spin = wx.SpinButton(self, -1, (270, 20), (h*2, h))
self.spin.SetRange(1980, 2010)
self.spin.SetValue(start_year)
self.Bind(wx.EVT_SPIN, self.OnSpin, self.spin)
# button for calendar dialog test
self.but1 = wx.Button(self, -1, "Test Calendar Dialog", (380, 80))
self.Bind(wx.EVT_BUTTON, self.TestDlg, self.but1)
# button for calendar window test
self.but2 = wx.Button(self, -1, "Test Calendar Window", (380, 180))
self.Bind(wx.EVT_BUTTON, self.TestFrame, self.but2)
self.but3 = wx.Button(self, -1, "Test Calendar Print", (380, 280))
self.Bind(wx.EVT_BUTTON, self.OnPreview, self.but3)
# calendar dialog
def TestDlg(self, event): # test the date dialog
dlg = wx.lib.calendar.CalenDlg(self)
dlg.Centre()
if dlg.ShowModal() == wx.ID_OK:
result = dlg.result
day = result[1]
month = result[2]
year = result[3]
new_date = str(month) + '/'+ str(day) + '/'+ str(year)
self.log.WriteText('Date Selected: %s\n' % new_date)
else:
self.log.WriteText('No Date Selected')
# calendar window test
def TestFrame(self, event):
frame = CalendFrame(self, -1, "Test Calendar", self.log)
frame.Show(True)
return True
# calendar print preview
def OnPreview(self, event):
month = self.calend.GetMonth()
year = self.calend.GetYear()
prt = PrintCalend(self.frame, month, year)
prt.Preview()
# month and year control events
def OnSpin(self, event):
year = event.GetPosition()
self.dtext.SetValue(str(year))
self.calend.SetYear(year)
self.calend.Refresh()
def EvtComboBox(self, event):
name = event.GetString()
self.log.WriteText('EvtComboBox: %s\n' % name)
monthval = self.date.FindString(name)
self.scroll.SetScrollbar(monthval, 1, 12, 1, True)
self.calend.SetMonth(monthval+1)
self.ResetDisplay()
def Scroll(self, event):
value = self.scroll.GetThumbPosition()
monthval = int(value)+1
self.calend.SetMonth(monthval)
self.ResetDisplay()
self.log.WriteText('Month: %s\n' % value)
name = wx.lib.calendar.Month[monthval]
self.date.SetValue(name)
# log mouse events
def MouseClick(self, evt):
text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
self.log.WriteText('Date Selected: ' + text + '\n')
# set the highlighted days for the calendar
def ResetDisplay(self):
month = self.calend.GetMonth()
try:
set_days = test_days[month]
except:
set_days = [1, 5, 12]
self.calend.AddSelect([4, 11], 'BLUE', 'WHITE')
self.calend.SetSelDay(set_days)
self.calend.Refresh()
# increment and decrement toolbar controls
def OnIncYear(self, event):
self.calend.IncYear()
self.ResetDisplay()
def OnDecYear(self, event):
self.calend.DecYear()
self.ResetDisplay()
def OnIncMonth(self, event):
self.calend.IncMonth()
self.ResetDisplay()
def OnDecMonth(self, event):
self.calend.DecMonth()
self.ResetDisplay()
def OnCurrent(self, event):
self.calend.SetCurrentDay()
self.ResetDisplay()
# test of full window calendar control functions
class CalendFrame(wx.Frame):
def __init__(self, parent, id, title, log):
wx.Frame.__init__(self, parent, id, title, size=(400, 400),
style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.log = log
self.CreateStatusBar()
self.mainmenu = wx.MenuBar()
menu = wx.Menu()
menu = self.MakeFileMenu()
self.mainmenu.Append(menu, '&File')
self.MakeToolMenu() # toolbar
self.SetMenuBar(self.mainmenu)
self.calend = wx.lib.calendar.Calendar(self, -1)
self.calend.SetCurrentDay()
self.calend.grid_color = 'BLUE'
self.calend.SetBusType()
# self.calend.ShowWeekEnd()
self.ResetDisplay()
self.Bind(wx.lib.calendar.EVT_CALENDAR, self.MouseClick, self.calend)
def MouseClick(self, evt):
text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
self.log.WriteText('Date Selected: ' + text + '\n')
def OnCloseWindow(self, event):
self.Destroy()
def ResetDisplay(self):
month = self.calend.GetMonth()
try:
set_days = test_days[month]
except:
set_days = [1, 5, 12]
self.calend.AddSelect([2, 16], 'GREEN', 'WHITE')
self.calend.SetSelDay(set_days)
self.calend.Refresh()
def OnIncYear(self, event):
self.calend.IncYear()
self.ResetDisplay()
def OnDecYear(self, event):
self.calend.DecYear()
self.ResetDisplay()
def OnIncMonth(self, event):
self.calend.IncMonth()
self.ResetDisplay()
def OnDecMonth(self, event):
self.calend.DecMonth()
self.ResetDisplay()
def OnCurrent(self, event):
self.calend.SetCurrentDay()
self.ResetDisplay()
def MakeFileMenu(self):
menu = wx.Menu()
mID = wx.NewId()
menu.Append(mID, 'Decrement', 'Next')
self.Bind(wx.EVT_MENU, self.OnDecMonth, id=mID)
mID = wx.NewId()
menu.Append(mID, 'Increment', 'Dec')
self.Bind(wx.EVT_MENU, self.OnIncMonth, id=mID)
menu.AppendSeparator()
mID = wx.NewId()
menu.Append(mID, 'E&xit', 'Exit')
self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=mID)
return menu
def MakeToolMenu(self):
tb = self.CreateToolBar(wx.TB_HORIZONTAL|wx.NO_BORDER)
mID = wx.NewId()
SetToolPath(self, tb, mID, images.getDbDecBitmap(), 'Dec Year')
self.Bind(wx.EVT_TOOL, self.OnDecYear, id=mID)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -