📄 calendar.py
字号:
key = self.sel_key + delta
self.SelectDay(key)
else:
self.month = newDate.GetMonth() + 1
self.year = newDate.GetYear()
self.set_day = newDate.GetDay()
self.sel_key = None
self.DoDrawing(wx.ClientDC(self))
event.Skip()
def SetSize(self, set_size):
self.size = set_size
def SetSelDay(self, sel):
# list of highlighted days
self.sel_lst = sel
# get the current date
def SetNow(self):
dt = now()
self.month = dt.month
self.year = dt.year
self.day = dt.day
# set the current day
def SetCurrentDay(self):
self.SetNow()
self.set_day = self.day
# get the date, day, month, year set in calendar
def GetDate(self):
return self.day, self.month, self.year
def GetDay(self):
return self.day
def GetMonth(self):
return self.month
def GetYear(self):
return self.year
# set the day, month, and year
def SetDayValue(self, day):
self.set_day = day
self.day = day
def SetMonth(self, month):
if month >= 1 and month <= 12:
self.month = month
else:
self.month = 1
self.set_day = None
def SetYear(self, year):
self.year = year
# increment year and month
def IncYear(self):
self.year = self.year + 1
self.set_day = None
def DecYear(self):
self.year = self.year - 1
self.set_day = None
def IncMonth(self):
self.month = self.month + 1
if self.month > 12:
self.month = 1
self.year = self.year + 1
self.set_day = None
def DecMonth(self):
self.month = self.month - 1
if self.month < 1:
self.month = 12
self.year = self.year - 1
self.set_day = None
# test to see if the selection has a date and create event
def TestDay(self, key):
try:
self.day = int(self.cal_days[key])
except:
return None
if self.day == "":
return None
else:
# Changed 12/1/03 by jmg (see above) to support 2.5 event binding
evt = wx.PyCommandEvent(wxEVT_COMMAND_PYCALENDAR_DAY_CLICKED, self.GetId())
evt.click, evt.day, evt.month, evt.year = self.click, self.day, self.month, self.year
evt.shiftkey = self.shiftkey
evt.ctrlkey = self.ctrlkey
self.GetEventHandler().ProcessEvent(evt)
self.set_day = self.day
return key
# find the clicked area rectangle
def GetDayHit(self, mx, my):
for key in self.rg.keys():
val = self.rg[key]
ms_rect = wx.Rect(mx, my, 1, 1)
if wx.IntersectRect(ms_rect, val) is not None:
result = self.TestDay(key)
return result
return None
# calendar drawing
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 SetTextAlign(self, vert, horz):
self.num_align_horz = horz
self.num_align_vert = vert
def AddSelect(self, list, font_color, back_color):
list_val = [list, font_color, back_color]
self.select_list.append(list_val)
def ShowWeekEnd(self):
# highlight weekend
self.show_weekend = True
def SetBusType(self):
self.cal_type = "BUS"
def OnSize(self, evt):
self.Refresh(False)
evt.Skip()
def OnPaint(self, event):
DC = wx.PaintDC(self)
self.DoDrawing(DC)
def DoDrawing(self, DC):
#DC = wx.PaintDC(self)
DC.BeginDrawing()
try:
cal = self.caldraw
except:
self.caldraw = CalDraw(self)
cal = self.caldraw
cal.hide_grid = self.hide_grid
cal.hide_title = self.hide_title
cal.show_weekend = self.show_weekend
cal.cal_type = self.cal_type
cal.outer_border = self.outer_border
cal.num_align_horz = self.num_align_horz
cal.num_align_vert = self.num_align_vert
cal.colors = self.colors
if self.size is None:
size = self.GetClientSize()
else:
size = self.size
# drawing attributes
cal.SetSize(size)
cal.SetCal(self.year, self.month)
# these have to set after SetCal as SetCal would overwrite them again.
cal.set_x_mrg = self.set_x_mrg
cal.set_y_mrg = self.set_y_mrg
cal.set_y_end = self.set_y_end
for val in self.select_list:
cal.AddSelect(val[0], val[1], val[2])
cal.DrawCal(DC, self.sel_lst)
self.rg = cal.GetRect()
self.cal_days = cal.GetCal()
self.st_pos = cal.GetOffset()
self.ymax = DC.MaxY()
if self.set_day != None:
self.SetDay(self.set_day)
DC.EndDrawing()
# draw the selection rectangle
def DrawFocusIndicator(self, draw):
DC = wx.ClientDC(self)
try:
if draw == True:
self.caldraw.DrawFocusIndicator(DC)
else:
self.caldraw.DrawBorder(DC,True)
except:
pass
def DrawRect(self, key, bgcolor = 'WHITE', fgcolor= 'PINK',width = 0):
if key == None:
return
DC = wx.ClientDC(self)
DC.BeginDrawing()
brush = wx.Brush(MakeColor(bgcolor))
DC.SetBrush(brush)
DC.SetPen(wx.TRANSPARENT_PEN)
rect = self.rg[key]
DC.DrawRectangle(rect.x+1, rect.y+1, rect.width-2, rect.height-2)
self.caldraw.DrawDayText(DC,key)
DC.EndDrawing()
def DrawRectOrg(self, key, fgcolor = 'BLACK', width = 0):
if key == None:
return
DC = wx.ClientDC(self)
DC.BeginDrawing()
brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT)
DC.SetBrush(brush)
try:
DC.SetPen(wx.Pen(MakeColor(fgcolor), width))
except:
DC.SetPen(wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), width))
rect = self.rg[key]
DC.DrawRectangleRect(rect)
DC.EndDrawing()
# set the day selection
def SetDay(self, day):
d = day + self.st_pos - 1
self.SelectDay(d)
def IsDayInWeekend(self, key):
try:
t = Date(self.year, self.month, 1)
day = self.cal_days[key]
day = int(day) + t.day_of_week
if day % 7 == 6 or day % 7 == 0:
return True
except:
return False
def SelectDay(self, key):
sel_size = 1
# clear large selection
if self.sel_key != None:
(cfont, bgcolor) = self.__GetColorsForDay(self.sel_key)
self.DrawRect(self.sel_key, bgcolor,cfont, sel_size)
self.DrawRect(key, self.GetColor(COLOR_HIGHLIGHT_BACKGROUND), self.GetColor(COLOR_HIGHLIGHT_FONT), sel_size)
# store last used by
self.sel_key = key
def ClearDsp(self):
self.Clear()
def SetMargin(self, xmarg, ymarg):
self.set_x_mrg = xmarg
self.set_y_mrg = ymarg
self.set_y_end = ymarg
def __GetColorsForDay(self, key):
cfont = self.GetColor(COLOR_FONT)
bgcolor = self.GetColor(COLOR_BACKGROUND)
if self.IsDayInWeekend(key) is True and self.show_weekend is True:
cfont = self.GetColor(COLOR_WEEKEND_FONT)
bgcolor = self.GetColor(COLOR_WEEKEND_BACKGROUND)
try:
dayIdx = int(self.cal_days[key])
(cfont, bgcolor) = self.caldraw.cal_sel[dayIdx]
except:
pass
return (cfont, bgcolor)
class CalenDlg(wx.Dialog):
def __init__(self, parent, month=None, day = None, year=None):
wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360))
self.result = None
# set the calendar and attributes
self.calend = Calendar(self, -1, (20, 60), (240, 200))
if month == None:
self.calend.SetCurrentDay()
start_month = self.calend.GetMonth()
start_year = self.calend.GetYear()
else:
self.calend.month = start_month = month
self.calend.year = start_year = year
self.calend.SetDayValue(day)
self.calend.HideTitle()
self.ResetDisplay()
# get month list from DateTime
monthlist = GetMonthList()
# select the month
self.date = wx.ComboBox(self, -1, Month[start_month], (20, 20), (90, -1),
monthlist, wx.CB_DROPDOWN)
self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)
# alternate spin button to control the month
h = self.date.GetSize().height
self.m_spin = wx.SpinButton(self, -1, (115, 20), (h*1.5, h), wx.SP_VERTICAL)
self.m_spin.SetRange(1, 12)
self.m_spin.SetValue(start_month)
self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin)
# spin button to control the year
self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1))
h = self.dtext.GetSize().height
self.y_spin = wx.SpinButton(self, -1, (225, 20), (h*1.5, h), wx.SP_VERTICAL)
self.y_spin.SetRange(1980, 2010)
self.y_spin.SetValue(start_year)
self.Bind(wx.EVT_SPIN, self.OnYrSpin, self.y_spin)
self.Bind(EVT_CALENDAR, self.MouseClick, self.calend)
x_pos = 50
y_pos = 280
but_size = (60, 25)
btn = wx.Button(self, wx.ID_OK, ' Ok ', (x_pos, y_pos), but_size)
self.Bind(wx.EVT_BUTTON, self.OnOk, btn)
btn = wx.Button(self, wx.ID_CANCEL, ' Close ', (x_pos + 120, y_pos), but_size)
self.Bind(wx.EVT_BUTTON, self.OnCancel, btn)
def OnOk(self, evt):
self.result = ['None', str(self.calend.day), Month[self.calend.month], str(self.calend.year)]
self.EndModal(wx.ID_OK)
def OnCancel(self, event):
self.EndModal(wx.ID_CANCEL)
# log the mouse clicks
def MouseClick(self, evt):
self.month = evt.month
# result click type and date
self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)]
if evt.click == 'DLEFT':
self.EndModal(wx.ID_OK)
# month and year spin selection routines
def OnMonthSpin(self, event):
month = event.GetPosition()
self.date.SetValue(Month[month])
self.calend.SetMonth(month)
self.calend.Refresh()
def OnYrSpin(self, event):
year = event.GetPosition()
self.dtext.SetValue(str(year))
self.calend.SetYear(year)
self.calend.Refresh()
def EvtComboBox(self, event):
name = event.GetString()
monthval = self.date.FindString(name)
self.m_spin.SetValue(monthval+1)
self.calend.SetMonth(monthval+1)
self.ResetDisplay()
# set the calendar for highlighted days
def ResetDisplay(self):
month = self.calend.GetMonth()
self.calend.Refresh()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -