Python Create calendar instance _python

Source: Internet
Author: User

This article describes the Python creation of the calendar method, and, unlike in the past, the example of this article does not use Python provided by the calendar implementation, I believe that the Python program for everyone to have some reference value.

This program in Windows test through, because the Python character encoding directly output to the operating system, so win under the GBK ANSI, Linux probably under the Utf-8 (not tested)

#coding =GBK #-*-coding:cp936-*-# make a calendar (show only the Gregorian date) ' implementation method: Do not use the calendar provided by Python, based on the date given: 1. The first day of the year is based on the input year (
Year + (YEAR-1)/4-(YEAR-1)/100 + (YEAR-1)/400)% 7) 2. Based on the date entered (it only takes years) to get this date in the current year of the day 3. The first day of the current month according to 1 and 2 is the week.
4. Create a calendar, in fact, in the 5x7 table in the pre-placed 5*7 label, respectively, representing 1-31 of the case (should contain all the cases).
5. Print 1-31 from the position you obtained, with 7 as a row.
6. Update the calendar and update the contents of the calendar when you work with the calendar header (changing the date). 7. The layout of the entire component is the 7x7 form.
The first line shows the calendar header, including the display and selection of the month and the year, and the second behavior displays the date, 3-7 is the monthly information displayed.
  "' class Calendar:pass appcal = Calendar () Import Time Def calcfirstdayofmonth (year,month,day):" The day of the week is counted "
    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 make a judgment on the year, the day is only added up and down, not based on the monthly judgment entered whether the legal if year < 0 or month < 0 or month > one or day < 0 or Day >31:import os os._exit (1) sum + = Day Leap = 0 if (year% = = 0) or (year% 4 = 0) and (ye Ar%!= 0)): Leap = 1 if (leap = 1) and (Month > 2): Sum + = 1 # First calculate a year'sFirst day of the Week # (year + (YEAR-1)/4-(YEAR-1)/100 + (year-1)/400)% 7 return (sum% 7-1 + (year + (YEAR-1)/4-) -1)/100 + (year-1)/400))% 7 def createmonth (master): ' Create calendar ' for I in Range (5): for J in Range (7): Lab El (Master,text = '). Grid (row = i + 2,column = j) def updatedate (): ' Update Calendar ' #得到当前选择的日期 year = Int (appcal.vyear.g  
  ET ()) 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 it is the year% = = 0) or ((year% 4 = 0), and (year%!= 0)): months[1] + = 1 FD = Calcfirstdayofmon Th (year,month,1) for I in range (5): for J in Range (7): Root.grid_slaves (i +2,j) [0][' text '] = ' for I-Ra Nge (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 ' # Get current date, set to Default 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, MA Ster,appcal.vyear) + tuple (range (2005,2010)) omyear.grid (row = 0,column = Col_idx); col_idx = 1 # Create month component APPCAL.VM
  Onth = 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 week label weeks = [' Sun. ', ' Mon. ', ' Tues. ', ' Wed. ', ' Thurs. ', ' Fri. ', ' the ' 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 and run the example of this article, and according to their own needs to improve and improve the code.

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.