Python to create a calendar instance

Source: Internet
Author: User
This article describes how Python creates the calendar, unlike in the past, the example of this article does not use the calendar implementation provided by Python, I believe that the Python program design for everyone has some reference value.

This program is tested under Windows, due to the Python character encoding directly output to the operating system, so win under the GBK ANSI, Linux under the quasi-utf-8 (not tested)

#coding =gbk#-*-coding:cp936-*-# make a calendar (only the Gregorian date) "Implementation method: Do not use the calendar provided by Python, calculated according to the date given: 1. The first day of the year based on the input year is the days of the week ( Year + (YEAR-1)/4-(YEAR-1)/100 + (YEAR-1)/400)% 7) 2. The date on which it was entered (only if it takes years) to get this date is given in the day of the current years 3. Based on 1 and 2, the first day of the current month is the days of the week. 4. Create a calendar, in fact, in the 5x7 table is pre-placed 5*7 labels, respectively, 1-31 of the situation (should contain all cases). 5. Print 1-31 from the position you obtained, with 7 as a line. 6. Update the calendar, and when the calendar header is manipulated (when the date is changed), the contents of the calendar display are updated. 7. The layout of the entire component is a tabular way of 7x7. The first line displays the calendar header, including the display and selection of the month and day; the second behavior displays the date, 3-7 is the month information that is displayed.  ' Class calendar:passappcal = Calendar () import timedef calcfirstdayofmonth (year,month,day): ' Calculate 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 ' # to judge the year and year, the day is only added upper and lower limits, not according to the month to determine whether the legal input if year < 0 or month < 0 or month > one or day < 0 or days >31:    Import OS os._exit (1) sum + = Day Leap = 0 if (year% of 0) or ((year% 4 = = 0) and (year% 100! = 0)): Leap = 1 if (leap = = 1) and (Month > 2): Sum + = 1 # First calculation of the first day of the year is the days of the Week # (YEAR-1)/4-(YEAR-1)/100 + (year-1)/400)% 7 return (sum% 7-1 + (year + (YEAR-1)/4-(YEAR-1)/100 + (year-1)/400))% 7def Crea Temonth (Master): "Create Calendar" ' for I in a range (5): for J in Range (7): Label (Master,text = "). Grid (row = i + 2,colum n = j) def updatedate (): "Update Calendar" #得到当前选择的日期 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] # to determine if the year% = = 0, or (year% 4 = = 0) and (year% + = 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 default value now = Time.localtime (Time.time ()) Col_idx = 0 # created years 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,colum n = 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))) Ommo Nth.grid (row = 0,column = Col_idx); Col_idx + = 1 # creation Year Component Appcal.vday = Stringvar () AppCal.vDay.set (now[2]) Label (maste R,text = ' Day '). Grid (row = 0,column = Col_idx), Col_idx + = 1 Omday = apply (Optionmenu, (master,appcal.vday) + tuple (range (1 )) Omday.grid (row = 0,column = Col_idx); Col_idx + = 1 # Create Update button btupdate = button (Master,text = ' Update ', command = up datedate) btupdate.grid (row = 0,column = Col_idx); Col_idx + = 1 # Print week 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 Imp Ort *root = Tk () drawheadER (root) createmonth (root) updatedate () Root.mainloop () 

Interested friends can debug and run a sample of this article, and according to their own needs to improve and improve the code.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.