This article mainly introduces how to create a calendar instance in Python instead of using the calendar implementation provided by Python. if you need it, refer to the following document to describe how to create a calendar in Python, the example in this article does not use the calendar implementation provided by Python. I believe it will be of some reference value to everyone's Python program design.
This program passed the test in windows. Since python character encoding is directly output to the operating system, gbk ansi is used in so win, and UTF-8 is used in linux (not tested)
# Coding = gbk #-*-coding: cp936-*-# Create a calendar (only display the calendar date) ''' implementation method: Do not use the calendar provided by python, calculated based on the given date: 1. the first day of the year is the day of the week (year + (year-1)/4-(year-1)/100 + (year-1)/400) based on the input year) % 7) 2. then, based on the input date (only the year and month are required), we can get the day 3 of the current year. the first day of the current month is the day of the week based on 1 and 2. 4. create a calendar. In fact, 5*7 labels are pre-placed in the table 5x7, indicating 1-31 (including all the conditions ). 5. print 1-31 from the obtained position, and use 7 as a line. 6. update the calendar. when the calendar header is operated (when the date is changed), the calendar display content is updated. 7. the layout of the entire component is 7x7. The first line shows the calendar header, including the display and selection of year, month, and day; the second row shows the date, and 3-7 shows the month information. '''Class Calendar: passAppCal = Calendar () import timedef calcFirstDayOfMonth (year, month, day): ''' calculates the number of months in a day of the week, 59,90, 120,151,181,212,243,273,304,334) if 0 <= month <= 12: sum = months [month-1] else: print 'data error' # judge the year and month, only the upper and lower limits are added for a day. if year <0 or month <0 or month> 11 or day <0 or day> 31: import OS. _ exit (1) sum + = day leap = 0 if (year % 400 = 0) or (ye Ar % 4 = 0) and (year %100! = 0): leap = 1 if (leap = 1) and (month> 2 ): sum + = 1 # calculate the day of the week on the first day of a year # (year + (year-1)/4-(year-1)/100 + (year-1) /400) % 7 return (sum % 7-1 + (year-1)/4-(year-1)/100 + (year-1) /400) % 7def createMonth (master): ''' Create Calendar ''' for I in range (5): for j in range (7): Label (master, text = ''). grid (row = I + 2, column = j) def updateDate (): ''' update calendar ''' # obtain the selected date year = int (AppCal. vYe Ar. get () month = int (AppCal. vMonth. get () day = int (AppCal. vDay. get () months = [400, 30, 31, 30, 31] # judge whether the year is Swiss if (year % = 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 the calendar header ''' # obtain the current date and set it to the default value now = time. localtime (time. time () col_idx = 0 # create the 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 () omYear. grid (row = 0, column = col_idx); col_idx + = 1 # Create a 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 an Update Button btUpdate = Button (master, text = 'update', command = updateDate) btUpdate. grid (row = 0, column = col_idx); col_idx + = 1 # print the weekly tag 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 ()
If you are interested, you can debug and run the examples in this article and improve the code as needed.