Create a calendar instance in Python and a calendar instance in python
This article describes how to create a calendar in Python. Different from the past, this example does not use the calendar implementation provided by Python. I believe it will be of some reference for 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.
A small calendar made of python
If you want to create a desktop application, you also need to install eric4
Similar to vc ++, you can select a control and add code.
Python class instantiation
1. In a python class, a private variable or function starts with _ (two underscores) but does not end with _. Private functions and variables cannot be called out of class.
Class test:
Def _ init _ (self, num ):
Self. _ num = num
PrivateTest = test (100)
PrivateTest. _ num # An error is returned.
Of course, there is also a way to adjust it, but it is not recommended to do that.
2. The variable s in the first class se () is a class variable, which can be accessed by the class itself, such as se. s, can also be accessed by various objects, and the value is unique because it exists in the class, a bit like static in C ++.
However, if an object also creates a variable named s that overwrites the class variable, the self. s is the property of the object and will not be adjusted to the class variable.
You can run it.
#-*-Coding: cp936 -*-
Class:
Name = []
Def _ init _ (self, name ):
Self. name. append (name)
Def nameMyself (self, name ):
Self. name = [name]
Print 'My name is ', self. name,' and class A1name is: ', A. name
Def test (self ):
Print "my name is", self. name
Obj = A ("num0 ")
Obj1 = A ("num1 ")
Print "obj1 'name", obj1.name # object metadata class variable name
Print "class A 'name", A. name # class variable name
Obj1.test () # The name Of The accessed class variable.
Obj1.nameMyself ('aid ') # Give yourself a name that overwrites the name of the class variable
Obj1.test () # For obj1, you can only access your own name.
Print "class a' name", A. name # The class variable still exists.