日历打印器 v3.1
对着《程序设计思想与方法》一步步抄出来的,边写边学,原来是python2的代码,对应python3进行了改写。
- 功能:输入年份(v3.0之前 1900之后),打印日历(v1.0 命令行 / v2.0及之后 txt文本文件)。
v1.0源代码:
# calendar printer v1.0
def getYear():
print("日历打印器 v1.0")
year = int(input("请输入要打印的年份(1900年之后):"))
return year
def firstDay(year):
k = leapyears(year)
n = (year - 1900) * 365 + k
return (n + 1) % 7
def leapyears(year):
count = 0
for y in range(1900,year):
if y%4 == 0 and (y%100 != 0 or y%400 == 0):
count = count + 1
return count
def printCalendar(year,w):
print
print("=========== " + str(year) + " ===========")
first = w
for month in range(12):
heading(month)
first = oneMonth(year,month,first)
def heading(m):
months = ["Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec"]
print(" %s " % (months[m]))
print("Mon Tue Wed Thu Fri Sat Sun")
def oneMonth(year,month,first):
d = days(year,month)
frame = layout(first,d)
printMonth(frame)
return (first + d) % 7
def days(y,m):
month_days = [31,28,31,30,31,30,31,31,30,31,30,31]
d = month_days[m]
if (m == 1) and (y%4 == 0 and (y%100 != 0 or y%400 == 0)):
d = d + 1
return d
def layout(first,d):
frame = 42 * [""]
if first == 0:
first = 7
j = first - 1
for i in range(1,d+1):
frame[j] = i
j = j + 1
return frame
def printMonth(frame):
for i in range(42):
print("%3s" % (frame[i]),end=" ")
if (i+1)%7 == 0:
print()
def main():
year = getYear()
print()
w = firstDay(year)
printCalendar(year,w)
print()
input("按回车退出...")
main()
v2.0源代码(加入GUI,输出txt文本文件):
# 日历打印器 v2.0
# calendar printer v2.0
# 内核
content = []
def firstDay(year):
k = leapyears(year)
n = (year - 1900) * 365 + k
return (n + 1) % 7
def leapyears(year):
count = 0
for y in range(1900,year):
if y%4 == 0 and (y%100 != 0 or y%400 == 0):
count = count + 1
return count
def printCalendar(year,w):
content.append("=========== " + str(year) + " ==========\n")
content.append('\n')
first = w
for month in range(12):
heading(month)
first = oneMonth(year,month,first)
def heading(m):
months = ["Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec"]
content.append(" %s \n" % (months[m]))
content.append("Mon Tue Wed Thu Fri Sat Sun\n")
def oneMonth(year,month,first):
d = days(year,month)
frame = layout(first,d)
printMonth(frame)
return (first + d) % 7
def days(y,m):
month_days = [31,28,31,30,31,30,31,31,30,31,30,31]
d = month_days[m]
if (m == 1) and (y%4 == 0 and (y%100 != 0 or y%400 == 0)):
d = d + 1
return d
def layout(first,d):
frame = 42 * [""]
if first == 0:
first = 7
j = first - 1
for i in range(1,d+1):
frame[j] = i
j = j + 1
return frame
def printMonth(frame):
for i in range(42):
content.append("%3s" % (frame[i]) + " ")
if (i+1)%7 == 0:
content.append('\n')
# 本地时间
import time
localtime = time.asctime( time.localtime( time.time() ) )
# 运行
def output():
global year
year = int(input.get())
w = firstDay(year)
printCalendar(year,w)
content.append('\n')
content.append("print time: " + localtime)
# 文件输出
def saveFile():
output()
file = open(str(year) + ".txt", "w")
for i in range(len(content)):
file.write(content[0])
del(content[0])
file.close()
# GUI界面
import tkinter as tk
def frame():
root = tk.Tk()
root.title("日历打印器 v2.0")
# root.iconbitmap(".\\\calendar.ico")
# root.geometry('640x360+200+200')
lb = tk.Label(root, text="日历打印器\n请输入要打印的年份(1900年之后):", bd=20, font=(20), fg="red", bg="yellow")
lb.grid(row=0,column=0,columnspan=2)
global input
input = tk.StringVar()
et = tk.Entry(root, textvariable = input)
et.grid(row=1,column=0,columnspan=2)
bt = tk.Button(root, text = "保存到当前目录", command = saveFile)
bt.grid(row=2,column=0)
quit = tk.Button(root, text = "退出", command = root.quit)
quit.grid(row=2,column=1)
root.mainloop()
frame()
v3.0源代码(用内置calendar模块简化代码,拓展到1900之前可用,GUI调整):
# 日历打印器 v3.0
# calendar printer v3.0
import calendar
from tkinter.constants import CENTER, GROOVE
# 本地时间
import time
localtime = time.strftime("%Z %Y-%m-%d %A %H:%M:%S", time.localtime())
# 文件输出
def saveFile():
year = int(input.get())
file = open(str(year) + ".txt", "w")
file.write(calendar.calendar(year,w=3,l=1,c=6) + '\n')
file.write('print time: ' + localtime)
file.close()
mb.showinfo('提示','输出成功!',icon = mb.INFO)
# GUI界面
import tkinter as tk
from tkinter import Frame, messagebox as mb
def gui(root):
fr = Frame(root, relief=GROOVE)
fr.place(relx=0.5, rely=0.5, anchor=CENTER)
lb = tk.Label(fr, text="日历打印器\n请输入要打印的年份:", bd=20, font=(20), fg="red", bg="yellow")
lb.grid(row=0,column=0,columnspan=2)
global input
input = tk.StringVar()
input.set(time.strftime('%Y'))
et = tk.Entry(fr, textvariable = input)
et.grid(row=1,column=0,columnspan=2)
bt = tk.Button(fr, text = "保存到当前目录", command = saveFile)
bt.grid(row=3,column=0)
quit = tk.Button(fr, text = "退出", command = root.quit)
quit.grid(row=3,column=1)
def main():
root = tk.Tk()
root.title("日历打印器 v3.0")
# root.iconbitmap(".\\\calendar.ico")
root.geometry('320x180+200+200')
gui(root)
root.mainloop()
main()
v3.1源代码(加入异常处理,完善程序结构):
# 日历打印器 v3.0
# calendar printer v3.0
# 本地时间
import time
def getTime():
localtime = time.strftime("%Z %Y-%m-%d %A %H:%M:%S", time.localtime())
return localtime
# 文件输出
import calendar
def saveFile():
try:
if type(eval(et.get())) == int:
year = int(et.get())
file = open(str(year) + ".txt", "w")
file.write(calendar.calendar(year,w=3,l=1,c=6) + '\n')
printTime = getTime()
file.write(u'打印时间:' + printTime)
file.close()
mb.showinfo('提示','输出成功!',icon = mb.INFO)
except:
et.delete(0,'end')
mb.showerror('提示','请正确输入!',icon = mb.ERROR)
# GUI界面
import tkinter as tk
from tkinter import messagebox as mb
def gui(root):
fr = tk.Frame(root, relief='groove')
fr.place(relx=0.5, rely=0.5, anchor='center')
lb = tk.Label(fr, text = "日历打印器\n\n请输入要打印的年份:", bd=20, font=(20), fg="red", bg="yellow")
lb.grid(row=0,column=0,columnspan=2)
global et
et = tk.Entry(fr)
et.insert(0,time.strftime('%Y'))
et.grid(row=1,column=0,columnspan=2)
bt = tk.Button(fr, text = "保存到当前目录", command = saveFile)
bt.grid(row=3,column=0)
quit = tk.Button(fr, text = "退出", command = root.quit)
quit.grid(row=3,column=1)
def main():
root = tk.Tk()
root.title("日历打印器 v3.0")
# root.iconbitmap(".\\\calendar.ico")
root.geometry('320x180+200+200')
gui(root)
root.mainloop()
main()
编译:
- 用
pyinstaller -F calendar_v1.0.py
方式打包生成exe(7.52M)。
- 用
pyinstaller -F calendar_v2.0.py
方式打包生成exe(10.1M)。
- 用
pyinstaller -F -w -i calendar.ico calendar_v3.0.py
方式打包生成calendar_v3.0.1.exe
(10.1M)。
- 用
pyinstaller -F -w -i calendar.ico calendar_v3.0.py --upx-dir <DIR>
方式打包生成calendar_v3.0.2.exe
(8.97M)。
- 用
pyinstaller -F -w -i calendar.ico calendar_v3.1.py
方式打包生成calendar_v3.1.1.exe
(10.1M)。
- 用
upx calendar_v3.1.1.exe
方式压缩生成calendar_v3.1.2.exe
(10.0M)。
- 用
pyinstaller -F -w -i calendar.ico calendar_v3.1.py --upx-dir <DIR>
方式打包生成calendar_v3.1.3.exe
(8.97M)。
- exe文件:https://wwx.lanzoui.com/b01ihdbmd (52pj)
截图:
其他:
周数可能还能实现,农历有点困难,毕竟calendar模块是没有农历的,我自己也不懂农历。
END