📄 calendar.py
字号:
#----------------------------------------------------------------------------
# Name: calendar.py
# Purpose: Calendar display control
#
# Author: Lorne White (email: lorne.white@telusplanet.net)
#
# Created:
# Version 0.92
# Date: Nov 26, 2001
# Licence: wxWindows license
#----------------------------------------------------------------------------
# 12/01/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o Updated for wx namespace
# o Tested with updated demo
# o Added new event type EVT_CALENDAR. The reason for this is that the original
# library used a hardcoded ID of 2100 for generating events. This makes it
# very difficult to fathom when trying to decode the code since there's no
# published API. Creating the new event binder might seem like overkill -
# after all, you might ask, why not just use a new event ID and be done with
# it? However, a consistent interface is very useful at times; also it makes
# it clear that we're not just hunting for mouse clicks -- we're hunting
# wabbit^H^H^H^H (sorry bout that) for calender-driven mouse clicks. So
# that's my sad story. Shoot me if you must :-)
# o There's still one deprecation warning buried in here somewhere, but I
# haven't been able to find it yet. It only occurs when displaying a
# print preview, and only the first time. It *could* be an error in the
# demo, I suppose.
#
# Here's the traceback:
#
# C:\Python\lib\site-packages\wx\core.py:949: DeprecationWarning:
# integer argument expected, got float
# newobj = _core.new_Rect(*args, **kwargs)
#
# 12/17/2003 - Jeff Grimmett (grimmtooth@softhome.net)
#
# o A few style-guide nips and tucks
# o Renamed wxCalendar to Calendar
# o Couple of bugfixes
#
# 06/02/2004 - Joerg "Adi" Sieker adi@sieker.info
#
# o Changed color handling, use dictionary instead of members.
# This causes all color changes to be ignored if they manipluate the members directly.
# SetWeekColor and other method color methods were adapted to use the new dictionary.
# o Added COLOR_* constants
# o Added SetColor method for Calendar class
# o Added 3D look of week header
# o Added colors for 3D look of header
# o Fixed width calculation.
# Because of rounding difference the total width and height of the
# calendar could be up to 6 pixels to small. The last column and row
# are now wider/taller by the missing amount.
# o Added SetTextAlign method to wxCalendar. This exposes logic
# which was already there.
# o Fixed CalDraw.SetMarg which set set_x_st and set_y_st which don't get used anywhere.
# Instead set set_x_mrg and set_y_mrg
# o Changed default X and Y Margin to 0.
# o Added wxCalendar.SetMargin.
#
# 17/03/2004 - Joerg "Adi" Sieker adi@sieker.info
# o Added keyboard navigation to the control.
# Use the cursor keys to navigate through the ages. :)
# The Home key function as go to today
# o select day is now a filled rect instead of just an outline
#
# 15/04/2005 - Joe "shmengie" Brown joebrown@podiatryfl.com
# o Adjusted spin control size/placement (On Windows ctrls were overlapping).
# o Set Ok/Cancel buttons to wx.ID_OK & wx.ID_CANCEL to provide default dialog
# behaviour.
# o If no date has been clicked clicked, OnOk set the result to calend's date,
# important if keyboard only navigation is used.
import wx
from CDate import *
CalDays = [6, 0, 1, 2, 3, 4, 5]
AbrWeekday = {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"}
_MIDSIZE = 180
COLOR_GRID_LINES = "grid_lines"
COLOR_BACKGROUND = "background"
COLOR_SELECTION_FONT = "selection_font"
COLOR_SELECTION_BACKGROUND = "selection_background"
COLOR_BORDER = "border"
COLOR_HEADER_BACKGROUND = "header_background"
COLOR_HEADER_FONT = "header_font"
COLOR_WEEKEND_BACKGROUND = "weekend_background"
COLOR_WEEKEND_FONT = "weekend_font"
COLOR_FONT = "font"
COLOR_3D_LIGHT = "3d_light"
COLOR_3D_DARK = "3d_dark"
COLOR_HIGHLIGHT_FONT = "highlight_font"
COLOR_HIGHLIGHT_BACKGROUND = "highlight_background"
BusCalDays = [0, 1, 2, 3, 4, 5, 6]
# Calendar click event - added 12/1/03 by jmg (see above)
wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED = wx.NewEventType()
EVT_CALENDAR = wx.PyEventBinder(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, 1)
def GetMonthList():
monthlist = []
for i in range(13):
name = Month[i]
if name != None:
monthlist.append(name)
return monthlist
def MakeColor(in_color):
try:
color = wxNamedColour(in_color)
except:
color = in_color
return color
def DefaultColors():
colors = {}
colors[COLOR_GRID_LINES] = 'BLACK'
colors[COLOR_BACKGROUND] = 'WHITE'
colors[COLOR_SELECTION_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
colors[COLOR_SELECTION_BACKGROUND] =wx.Colour(255,255,225)
colors[COLOR_BORDER] = 'BLACK'
colors[COLOR_HEADER_BACKGROUND] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE)
colors[COLOR_HEADER_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
colors[COLOR_WEEKEND_BACKGROUND] = 'LIGHT GREY'
colors[COLOR_WEEKEND_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
colors[COLOR_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
colors[COLOR_3D_LIGHT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT)
colors[COLOR_3D_DARK] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNSHADOW)
colors[COLOR_HIGHLIGHT_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
colors[COLOR_HIGHLIGHT_BACKGROUND] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT)
return colors
# calendar drawing routing
class CalDraw:
def __init__(self, parent):
self.pwidth = 1
self.pheight = 1
try:
self.scale = parent.scale
except:
self.scale = 1
self.gridx = []
self.gridy = []
self.DefParms()
def DefParms(self):
self.num_auto = True # auto scale of the cal number day size
self.num_size = 12 # default size of calendar if no auto size
self.max_num_size = 12 # maximum size for calendar number
self.num_align_horz = wx.ALIGN_CENTRE # alignment of numbers
self.num_align_vert = wx.ALIGN_CENTRE
self.num_indent_horz = 0 # points indent from position, used to offset if not centered
self.num_indent_vert = 0
self.week_auto = True # auto scale of week font text
self.week_size = 10
self.max_week_size = 12
self.colors = DefaultColors()
self.font = wx.SWISS
self.bold = wx.NORMAL
self.hide_title = False
self.hide_grid = False
self.outer_border = True
self.title_offset = 0
self.cal_week_scale = 0.7
self.show_weekend = False
self.cal_type = "NORMAL"
def SetWeekColor(self, font_color, week_color):
# set font and background color for week title
self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
self.colors[COLOR_3D_DARK] = MakeColor(week_color)
def SetSize(self, size):
self.set_sizew = size[0]
self.set_sizeh = size[1]
def InitValues(self): # default dimensions of various elements of the calendar
self.rg = {}
self.cal_sel = {}
self.set_cy_st = 0 # start position
self.set_cx_st = 0
self.set_y_mrg = 1 # start of vertical draw default
self.set_x_mrg = 1
self.set_y_end = 1
def SetPos(self, xpos, ypos):
self.set_cx_st = xpos
self.set_cy_st = ypos
def SetMarg(self, xmarg, ymarg):
self.set_x_mrg = xmarg
self.set_y_mrg = ymarg
self.set_y_end = ymarg
def InitScale(self): # scale position values
self.sizew = int(self.set_sizew * self.pwidth)
self.sizeh = int(self.set_sizeh * self.pheight)
self.cx_st = int(self.set_cx_st * self.pwidth) # draw start position
self.cy_st = int(self.set_cy_st * self.pheight)
self.x_mrg = int(self.set_x_mrg * self.pwidth) # calendar draw margins
self.y_mrg = int(self.set_y_mrg * self.pheight)
self.y_end = int(self.set_y_end * self.pheight)
def DrawCal(self, DC, sel_lst=[]):
self.InitScale()
self.DrawBorder(DC)
if self.hide_title is False:
self.DrawMonth(DC)
self.Center()
self.DrawGrid(DC)
self.GetRect()
if self.show_weekend is True: # highlight weekend dates
self.SetWeekEnd()
self.AddSelect(sel_lst) # overrides the weekend highlight
self.DrawSel(DC) # highlighted days
self.DrawWeek(DC)
self.DrawNum(DC)
def AddSelect(self, list, cfont=None, cbackgrd = None):
if cfont is None:
cfont = self.colors[COLOR_SELECTION_FONT] # font digit color
if cbackgrd is None:
cbackgrd = self.colors[COLOR_SELECTION_BACKGROUND] # select background color
for val in list:
self.cal_sel[val] = (cfont, cbackgrd)
# draw border around the outside of the main display rectangle
def DrawBorder(self, DC, transparent = False):
if self.outer_border is True:
if transparent == False:
brush = wx.Brush(MakeColor(self.colors[COLOR_BACKGROUND]), wx.SOLID)
else:
brush = wx.TRANSPARENT_BRUSH
DC.SetBrush(brush)
DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BORDER])))
# full display window area
rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)
DC.DrawRectangleRect(rect)
def DrawFocusIndicator(self, DC):
if self.outer_border is True:
DC.SetBrush(wx.TRANSPARENT_BRUSH)
DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_HIGHLIGHT_BACKGROUND]), style=wx.DOT))
# full display window area
rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)
DC.DrawRectangleRect(rect)
def DrawNumVal(self):
self.DrawNum()
# calculate the calendar days and offset position
def SetCal(self, year, month):
self.InitValues() # reset initial values
self.year = year
self.month = month
day = 1
t = Date(year, month, day)
dow = self.dow = t.day_of_week # start day in month
dim = self.dim = t.days_in_month # number of days in month
if self.cal_type == "NORMAL":
start_pos = dow+1
else:
start_pos = dow
self.st_pos = start_pos
self.cal_days = []
for i in range(start_pos):
self.cal_days.append('')
i = 1
while i <= dim:
self.cal_days.append(str(i))
i = i + 1
self.end_pos = start_pos + dim - 1
return start_pos
def SetWeekEnd(self, font_color=None, backgrd = None):
if font_color != None:
self.SetColor(COLOR_WEEKEND_FONT, MakeColor(font_color))
if backgrd != None:
self.SetColor(COLOR_WEEKEND_BACKGROUND, MakeColor(backgrd))
date = 6 - int(self.dow) # start day of first saturday
while date <= self.dim:
self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND)) # Saturday
date = date + 1
if date <= self.dim:
self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND)) # Sunday
date = date + 6
else:
date = date + 7
# get the display rectange list of the day grid
def GetRect(self):
cnt = 0
h = 0
w = 0
for y in self.gridy[1:-1]:
if y == self.gridy[-2]:
h = h + self.restH
for x in self.gridx[:-1]:
assert type(y) == int
assert type(x) == int
w = self.cellW
h = self.cellH
if x == self.gridx[-2]:
w = w + self.restW
rect = wx.Rect(x, y, w+1, h+1) # create rect region
self.rg[cnt] = rect
cnt = cnt + 1
return self.rg
def GetCal(self):
return self.cal_days
def GetOffset(self):
return self.st_pos
# month and year title
def DrawMonth(self, DC):
month = Month[self.month]
sizef = 11
if self.sizeh < _MIDSIZE:
sizef = 10
f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
DC.SetFont(f)
tw,th = DC.GetTextExtent(month)
adjust = self.cx_st + (self.sizew-tw)/2
DC.DrawText(month, adjust, self.cy_st + th)
year = str(self.year)
tw,th = DC.GetTextExtent(year)
adjust = self.sizew - tw - self.x_mrg
self.title_offset = th * 2
f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
DC.SetFont(f)
DC.DrawText(year, self.cx_st + adjust, self.cy_st + th)
def DrawWeek(self, DC): # draw the week days
# increase by 1 to include all gridlines
width = self.gridx[1] - self.gridx[0] + 1
height = self.gridy[1] - self.gridy[0] + 1
rect_w = self.gridx[-1] - self.gridx[0]
f = wx.Font(10, self.font, wx.NORMAL, self.bold) # initial font setting
if self.week_auto == True:
test_size = self.max_week_size # max size
test_day = ' Sun '
while test_size > 2:
f.SetPointSize(test_size)
DC.SetFont(f)
tw,th = DC.GetTextExtent(test_day)
if tw < width and th < height:
break
test_size = test_size - 1
else:
f.SetPointSize(self.week_size) # set fixed size
DC.SetFont(f)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -