日期计算器
from tkinter import *
root = Tk()
root.minsize(470,210)
root.title('日期计算器')
lable_title = Label(root,text="请输入日期,计算从元旦到该日期的天数:",font=('宋体,20'))
lable_title.place(x=30,y=20)
text_year = Entry(root,width=6)
text_year.place(x=60,y=70)
label_year = Label(root,text="年")
label_year.place(x=120,y=70)
text_month = Entry(root,width=6)
text_month.place(x=150,y=70)
label_month = Label(root,text="月")
label_month.place(x=210,y=70)
text_day = Entry(root,width=6)
text_day.place(x=240,y=70)
label_day = Label(root,text="日")
label_day.place(x=300,y=70)
msg_text = StringVar()
date_text = StringVar()
label_msg = Label(root,textvariable=msg_text,font=('宋体,20'))
label_msg.place(x=100,y=170,height=25)
label_sumDay = Label(root,textvariable=date_text,font=('宋体,20'),fg='red')
label_sumDay.place(x=200,y=170,height=25)
def btn_Cal():
nYear = int(text_year.get())
nMonth = int(text_month.get())
nDay = int(text_day.get())
nSumDays = totalDays(nYear,nMonth,nDay)
msg_text.set('总天数为:')
date_text.set(nSumDays)
def btn_Clr():
text_year.delete(0,END)
text_month.delete(0,END)
text_day.delete(0,END)
msg_text.set('')
date_text.set('')
text_year.focus()
def isLeapYear(nYear):
if((nYear % 4 == 0 & nYear % 100 != 0) or (nYear % 400 ==0)):
return 1
else:
return 0
def totalDays(nYear,nMonth,nDay):
nSumDays = 0
i = 1
while i < nMonth:
if i == 1 or i == 3 or i == 5 or i == 7 or i == 8 or i == 10 or i == 12:
nMonthDay = 31
elif i == 4 or i == 6 or i == 9 or i == 11:
nMonthDay = 30
elif i == 2:
if 1 == isLeapYear(nYear):
nMonthDay = 29
else:
nMonthDay = 28
else:
print("输入错误")
break
nSumDays = nSumDays + nMonthDay
i += 1
nSumDays = nSumDays + nDay
return nSumDays
btn_Cal = Button(root,text="计算",command=btn_Cal)
btn_Cal.place(x=80,y=120,width=80)
btn_Clr = Button(root,text="重置",command=btn_Clr)
btn_Clr.place(x=200,y=120,width=80)
root.mainloop()