Python 日期计算器

日期计算器

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())

	#调用totalDays()函数获取总天数
	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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值