Python creates a calendar instance
- 2020-04-02 13:58:13
- OfStack
This article describes how to create a calendar in Python. Different from the past, this article does not use the calendar implementation provided by Python.
This program passed the test under Windows. Since the python character encoding is directly output to the operating system, GBK ANSI shall prevail in so win, and utf-8 shall prevail in Linux (not tested).
#coding=gbk
# -*- coding: cp936 -*-
# Make a calendar ( Solar calendar date only)
''' Implementation method: not used python To provide the calendar According to the given date:
1. According to the input year, the first day of the year is the day of the week ( (year + (year - 1)/4 - (year - 1)/100 + (year -1)/400)% 7 )
2. Depending on the date you entered (just the year), you get the date and the day of the current year
3. According to the 1 and 2 Get the day of the week for the first day of the current month.
4. Create a calendar 5x7 In advance 5*7 a Label , respectively 1 - 31 Should include all cases.
5. will 1 - 31 From the position obtained start printing out to 7 As a line.
6. Update the calendar. When you manipulate the calendar header (changing the date), you update what the calendar shows.
7. The layout of the entire component is 7x7 Tabular mode. The first line displays the calendar head, including the display and selection of year, month and day; The second action shows the date, 3-7 Is the month information shown.
'''
class Calendar:
pass
AppCal = Calendar()
import time
def calcFirstDayOfMonth(year,month,day):
''' The day of the week '''
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 <= month <= 12:
sum = months[month - 1]
else:
print 'data error'
# Has made the judgment to the year, the day just added the upper and lower limits, did not judge according to the month whether the input is legal
if year < 0 or month < 0 or month > 11 or day < 0 or day >31:
import os
os._exit(1)
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
# Count the days of the week for the first day of the year
# (year + (year - 1)/4 - (year - 1)/100 + (year -1)/400)% 7
return (sum % 7 - 1 + (year + (year - 1)/4 - (year - 1)/100 + (year -1)/400))% 7
def createMonth(master):
''' Create a calendar '''
for i in range(5):
for j in range(7):
Label(master,text = '').grid(row = i + 2,column = j)
def updateDate():
''' Update the calendar '''
# Gets the currently selected date
year = int(AppCal.vYear.get())
month = int(AppCal.vMonth.get())
day = int(AppCal.vDay.get())
months = [31,28,31,30,31,30,31,31,30,31,30,31]
# Decide if it's a lucky year
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
months[1] += 1
fd = calcFirstDayOfMonth(year,month,1)
for i in range(5):
for j in range(7):
root.grid_slaves(i +2,j)[0]['text'] = ''
for i in range(1,months[month - 1] + 1):
root.grid_slaves((i + fd - 1)/7 + 2,(i + fd -1)%7)[0]['text'] = str(i)
def drawHeader(master):
''' Add calendar header '''
# Gets the current date, set to the default value
now = time.localtime(time.time())
col_idx = 0
# Create year component
AppCal.vYear = StringVar()
AppCal.vYear.set(now[0])
Label(master,text = 'YEAR').grid(row = 0,column = col_idx);col_idx += 1
omYear = apply(OptionMenu,(master,AppCal.vYear) + tuple(range(2005,2010)))
omYear.grid(row = 0,column = col_idx);col_idx += 1
# Create month component
AppCal.vMonth = StringVar()
AppCal.vMonth.set(now[1])
Label(master,text = 'Month').grid(row = 0,column = col_idx);col_idx += 1
omMonth = apply(OptionMenu,(master,AppCal.vMonth) + tuple(range(1,12)))
omMonth.grid(row = 0,column = col_idx);col_idx += 1
# Create year component
AppCal.vDay = StringVar()
AppCal.vDay.set(now[2])
Label(master,text = 'DAY').grid(row = 0,column = col_idx);col_idx += 1
omDay = apply(OptionMenu,(master,AppCal.vDay) + tuple(range(1,32)))
omDay.grid(row = 0,column = col_idx);col_idx += 1
# Create update button
btUpdate = Button(master,text = 'Update',command = updateDate)
btUpdate.grid(row = 0,column = col_idx);col_idx += 1
# Print weekly label
weeks = ['Sun.','Mon.','Tues.','Wed.','Thurs.','Fri.','Sat.']
for week in weeks:
Label(master,text = week).grid(row = 1,column = weeks.index(week))
from Tkinter import *
root = Tk()
drawHeader(root)
createMonth(root)
updateDate()
root.mainloop()
Interested friends can debug run this article example, and according to their own needs to improve and perfect the code.