废话不多说,直接上代码

import tkinter
import datetime
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox#获取今天的日期
today = datetime.date.today()#判断输入的日期是否合法
def is_date(date):try:datetime.datetime.strptime(date,"%Y-%m-%d")return Trueexcept:return False#计算两个日期的差的主函数
def riqichajisuan():riqi1=u1.get()riqi2=p1.get()if is_date(riqi1) and is_date(riqi2):date1 = datetime.datetime.strptime(riqi1,"%Y-%m-%d")date2 = datetime.datetime.strptime(riqi2,"%Y-%m-%d")date_difference=date2-date1result=date_difference.daysb=messagebox.showinfo('计算结果','这两个日期之间相差:'+ str(result) + '天')elif riqi1 == '' and riqi2 == '':sjk=messagebox.showerror('错误','两个日期均未输入或输入格式有误\n请重新输入正确格式的日期!')        elif riqi1 == '' :c=messagebox.showerror('错误','日期一未输入或输入格式有误\n请重新输入正确格式的日期!')elif riqi2 == '' :cqw=messagebox.showerror('错误','日期二未输入或输入格式有误\n请重新输入正确格式的日期!')else:a=messagebox.showerror('错误','输入的日期格式有误\n请重新输入正确格式的日期!')#计算两个日期的差的主窗体
def riqicha():def tuichu():top1.destroy()root2.state('normal')root2.state('icon')top1=Toplevel()top1.title('计算日期差')top1.resizable(width=False, height=False)width = 200heigh = 140screenwidth = top1.winfo_screenwidth()screenheight = top1.winfo_screenheight()top1.geometry('%dx%d+%d+%d'%(width, heigh, (screenwidth-width)/2, (screenheight-heigh)/2 - 70))tkinter.Label(top1,text='请在下面输入需要计算的日期:').place(width=200,height=30,x=0,y=3)tkinter.Label(top1,text='格式:年-月-日 如:2022-1-1').place(width=200,height=20,x=0,y=23)tkinter.Label(top1,text='日期一:').place(x=3,y=43)tkinter.Label(top1,text='日期二:').place(x=3,y=73)today = datetime.date.today()global u1u1=StringVar()b=tkinter.Entry(top1,textvariable=u1,width=20).place(x=52,y=43)global p1p1=StringVar()c=tkinter.Entry(top1,textvariable=p1,width=20).place(x=52,y=73)Button(top1,text='计算',command=riqichajisuan).place(width=60,height=30,x=23,y=101)Button(top1,text='退出',command=tuichu).place(width=60,height=30,x=117,y=101)#判断日期属于第几周星期几的计算函数
def panduanxingqi():riqi=u2.get()if is_date(riqi):date = datetime.datetime.strptime(riqi,"%Y-%m-%d")calendar = datetime.date.isocalendar(date)result1 = '该日期是{}年第{}周星期{}'.format(calendar[0],calendar[1],calendar[2])d=messagebox.showinfo('计算结果',result1)elif riqi == '':e=messagebox.showerror('错误','未输入任何日期\n请重新输入正确格式的日期!')else:f=messagebox.showerror('错误','输入的日期格式有误\n请重新输入正确格式的日期!')#判断日期属于第几周星期几的主窗体
def panxingqi():def tuichu():top2.destroy()root2.state('normal')root2.state('icon')top2=Toplevel()top2.title('判断星期几')top2.resizable(width=False, height=False)width = 200heigh = 140screenwidth = top2.winfo_screenwidth()screenheight = top2.winfo_screenheight()top2.geometry('%dx%d+%d+%d'%(width, heigh, (screenwidth-width)/2, (screenheight-heigh)/2 - 70))tkinter.Label(top2,text='请在下面输入需要计算的日期:').place(width=200,height=30,x=0,y=3)tkinter.Label(top2,text='格式:年-月-日 如:2022-1-1').place(width=200,height=20,x=0,y=23)tkinter.Label(top2,text='日期:').place(x=3,y=53)today = datetime.date.today()global u2u2=StringVar()b=tkinter.Entry(top2,textvariable=u2,width=20).place(x=52,y=53)Button(top2,text='计算',command=panduanxingqi).place(width=60,height=30,x=23,y=101)Button(top2,text='退出',command=tuichu).place(width=60,height=30,x=117,y=101)#判断日期是该年的第几天的计算函数
def panduantianshu():riqi3=u3.get()if is_date(riqi3):y,m,d=map(int,riqi3.split("-"))D=0days=[31,0,31,30,31,30,31,31,30,31,30,31]if ((y%400==0)|((y%4==0)&(y%100!=0))):days[1]=29else:days[1]=28for i in range(m-1):D=D+days[i]result2 = "该日期是{}年的第{}天".format(y,D+d)res=messagebox.showinfo('计算结果',result2)elif riqi3 == '':ero=messagebox.showerror('错误','未输入任何日期\n请重新输入正确格式的日期!')else:op=messagebox.showerror('错误','输入的日期格式有误\n请重新输入正确格式的日期!')#判断日期是该年的第几天的主窗体
def pantianshu():def tuichu():top3.destroy()root2.state('normal')root2.state('icon')top3=Toplevel()top3.title('判断第几天')top3.resizable(width=False, height=False)width = 200heigh = 140screenwidth = top3.winfo_screenwidth()screenheight = top3.winfo_screenheight()top3.geometry('%dx%d+%d+%d'%(width, heigh, (screenwidth-width)/2, (screenheight-heigh)/2 - 70))tkinter.Label(top3,text='请在下面输入需要计算的日期:').place(width=200,height=30,x=0,y=3)tkinter.Label(top3,text='格式:年-月-日 如:2022-1-1').place(width=200,height=20,x=0,y=23)tkinter.Label(top3,text='日期:').place(x=3,y=53)today = datetime.date.today()global u3u3=StringVar()b=tkinter.Entry(top3,textvariable=u3,width=20).place(x=52,y=53)Button(top3,text='计算',command=panduantianshu).place(width=60,height=30,x=23,y=101)Button(top3,text='退出',command=tuichu).place(width=60,height=30,x=117,y=101)#判断用户选择的模式
def panduan():if v.get()==mode[0]:a=messagebox.showerror('错误','您没有选择任何功能\n请重新选择!')if v.get()==mode[1]:riqicha()root2.state('icon')if v.get()==mode[2]:panxingqi()root2.state('icon')if v.get()==mode[3]:pantianshu()root2.state('icon')root2=Tk()
root2.title('日期计算器')
width = 200
heigh = 120
screenwidth = root2.winfo_screenwidth()
screenheight = root2.winfo_screenheight()
root2.geometry('%dx%d+%d+%d'%(width, heigh, (screenwidth-width)/2, (screenheight-heigh)/2 - 70))
tkinter.Label(root2,text='选择你需要的功能:').place(x=50,y=1,width=110,height=30)
mode=("点此选择","计算两个日期的差","判断日期属于第几周星期几","判断日期是该年的第几天")
v = StringVar(root2)
om = OptionMenu(root2,v,*mode).place(width=190,height=30,x=5,y=40)
Button(root2,text="确定",command=panduan).place(width=70,height=30,x=20,y=80)
Button(root2,text="退出",command=root2.destroy).place(width=70,height=30,x=110,y=80)
root2.mainloop()

运行效果:

Python用Tkinter实现日期计算器相关推荐

  1. python实现简单的日期计算器

    因为一直要计算当天日期往前推N天的日期,所以想做一个简单的日期计算器实现这个功能. 另外把计算两个日期之间的天数也一起给加进去了,就更完整. 这个日期计算器的界面很简陋,功能也非常简单. 暂时没有做通 ...

  2. python写科学计算器代码_Python编程使用tkinter模块实现计算器软件完整代码示例...

    Python编程使用tkinter模块实现计算器软件完整代码示例 来源:中文源码网    浏览: 次    日期:2018年9月2日 Python编程使用tkinter模块实现计算器软件完整代码示例 ...

  3. 使用Python Tkinter的年龄计算器应用程序

    如果您想使用Tkinter创建美观的应用程序,那么您来对地方了.在本文中,让我们使用Python的Tkinter库创建一个年龄计算器应用程序. 在这个年龄计算器应用程序中,用户可以输入他们的出生日期, ...

  4. Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(计算器简单功能)

    Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(计算器简单功能) 导读 动态演示调用python库的tkinter带你进入GUI世界(计算器简单功能) 目录 ...

  5. python tkinter计算器实例_Python编程使用tkinter模块实现计算器软件完整代码示例

    Python 提供了多个图形开发界面的库.Tkinter就是其中之一. Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 .Tk 和 Tkinter 可以在大多数 ...

  6. 80行代码使用Python+tkinter实现一个计算器

    闲话不说,直奔主题.建议大家跟着敲一遍代码,体会一下代码复用.字符串方法的运用和动态创建组件的妙处,然后在这个框架的基础上进行补充和发挥. 选择任何一款Python开发环境,创建一个程序文件,命名为t ...

  7. python的Tkinter库简单应用——开发一个简易计算器

    利用python的Tkinter库开发一个简易计算器 文章目录 利用python的Tkinter库开发一个简易计算器 前言 一.实验准备 二.开发步骤步骤 1.引入库 2.界面设计 3.关键--实现T ...

  8. python日期计算器 青少年编程电子学会python编程等级考试二级真题解析2021年12月

    目录 python日期计算器 一.题目要求 1.编程实现 2.输入输出 3.评分标准

  9. Python 日期计算器

    日期计算器 from tkinter import *#创建窗体 root = Tk() root.minsize(470,210) root.title('日期计算器')#控件名 lable_tit ...

最新文章

  1. 另存为里面没有jpg_选用打印捕捉功能将 SolidWorks 画面另存为JPG格式高清大图
  2. CBA 赛程的笔记 - 北京首钢
  3. 动态内存分配与柔性数组
  4. nginx主配置文件 在那找怎么打开
  5. Android 教程
  6. boost::mp11::mp_map_erase相关用法的测试程序
  7. Authid current_user的用法
  8. H5中 video 使用border-radius失效解决方法
  9. Android Architecture Blueprints----安卓架构详细分析
  10. HTML 空格转义符的用法
  11. {MySQL数据库初识}一 数据库概述 二 MySQL介绍 三 MySQL的下载安装、简单应用及目录介绍 四 root用户密码设置及忘记密码的解决方案 五 修改字符集编码 六 初识sql语句...
  12. Altium Designer元件库常见获取方法
  13. 基于NSGA3的swat模型情景的多目标优化算法,python
  14. 常见下载方式之BT下载实现过程详解
  15. 美因基因冲刺港交所:黄金赛道的“双冠王”
  16. huggingFace 中文模型实战——中文文本分类
  17. 【C生万物】 函数递归篇
  18. JGG论坛:赵方庆研究员解析肠道菌群与人体健康(11月10日10:00)
  19. VBA,用VBA进行分列(拆分列)的2种方法
  20. 2011高清影音产品市场日渐成熟 产业要求更高

热门文章

  1. android 人生日历,人生日历Android版新增日子,从此生活“拍”着过
  2. UltralSo制作U盘镜像,出现“设备忙,请关闭其他应用程序”的处理办法
  3. 一文详解:双向ESD二极管型号及选型
  4. 21、浏览器兼容性问题
  5. JavaScript中Math的用法
  6. tomcat是什么?通俗解释
  7. 推荐一份PHP程序员笔试题(附答案)
  8. 华为荣耀10是Android,华为荣耀10怎么样
  9. 2011的n次方的后四位
  10. 虚拟化基础-华为云计算虚拟化