Python制作简易版小工具之计算天数的实现思路

作者:大梦三千秋 时间:2023-10-29 08:12:07 

需求

给定一个日期,格式如 “2020-2-12”,计算出这个日期是 2020 年的第几天?

实现思路

  1. 使用 tkinter 和 tkinter.ttk 对界面进行布置;

  2. 使用 calendar 计算天数;

  3. 规范输入日期的格式;

  4. 对月份,天数进行逻辑判断;

  5. 输入错误抛出异常提示。

代码实现


# -*- coding: utf-8 -*-
'''
@File: calc_day_v2.py
@Time: 2020/02/12 20:33:22
@Author: 大梦三千秋
@Contact: yiluolion@126.com
'''
# Put the import lib here
from tkinter import *
import tkinter.messagebox as messagebox
from tkinter import ttk
import calendar
class MyException(BaseException):
 '''自定义异常类
 '''
 def __init__(self, message):
   self.message = message
def calculate(*args):
 '''计算天数方法
 '''
 try:
   # 用以存储天数
   nums = 0
   # 获取输入框中的数据
   year, month, day = [int(elem) for elem in date.get().split('-')]
   # 判断月份,规定在 1-12 之间
   if 1 <= month <= 12:
     # 遍历计算天数
     for month_x in range(1, month + 1):
       # 计算每月的天数
       _, month_day = calendar.monthrange(year, month_x)
       # 遍历的月份等于当前月份,对天数进行规整
       if month_x == month:
         # 文本输入框给出的天数不符合,则抛出异常
         if day > month_day:
           raise MyException("信息输入错误,注意天数!")
         continue
       nums += month_day
     nums += day
     # 设置值到文本框
     days.set(nums)
     the_year.set(year)
   else: # 月份超出范围抛出异常
     raise MyException("信息输入错误,注意月份!")
 except MyException as e:
   messagebox.showinfo(title="输入信息错误", message=e)
 except Exception as e:
   # print(e)
   messagebox.showinfo(title="输入信息错误", message="输出格式错误,按照 2020-2-12 这样的格式输入。注意月份,天数!")
root = Tk()
root.title("计算天数")
# 设置框架
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, S, E, W))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
date = StringVar()
the_year = StringVar()
days = StringVar()
# 文本框部件布置
date_entry = ttk.Entry(mainframe, width=10, textvariable=date)
date_entry.grid(column=2, row=1, sticky=(W, E))
# 标签及按钮的布置
ttk.Label(mainframe, text="例如:2020-2-12").grid(column=5, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=days).grid(column=4, row=2, sticky=(W, E))
ttk.Label(mainframe, textvariable=the_year).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=5, row=3)
ttk.Label(mainframe, text="日期:").grid(column=1, row=1, sticky=E)
ttk.Label(mainframe, text="这一天是").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="年的第").grid(column=3, row=2, sticky=E)
ttk.Label(mainframe, text="天").grid(column=5, row=2, sticky=W)
# 设置内边距
for child in mainframe.winfo_children():
 child.grid_configure(padx=5, pady=5)
date_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()

使用效果

正确输入的效果如下:

 Python制作简易版小工具之计算天数的实现思路

未按照格式输入,错误提示效果:

 Python制作简易版小工具之计算天数的实现思路

月份输入错误,提示效果如下:

 Python制作简易版小工具之计算天数的实现思路

天数超出当月范围的错误提示效果:

 Python制作简易版小工具之计算天数的实现思路

本篇的内容主要是对昨天的 tkinter 模块的延展使用,实现一个计算天数的小工具。

以上所述是小编给大家介绍的Python制作简易版小工具之计算天数的实现思路,希望对大家有所帮助!

来源:https://segmentfault.com/a/1190000021734857

标签:python,计算,天数
0
投稿

猜你喜欢

  • 使用tensorflow实现线性回归

    2023-08-14 02:33:55
  • Oracle 处理json数据的方法

    2024-01-16 15:11:15
  • 跨浏览器的本地存储(二):DOM:Storage

    2008-08-15 13:39:00
  • Python绘制堆叠柱状图的实例

    2022-01-04 06:14:55
  • 一段ASP单页显示文件夹下所有图片的代码

    2011-02-28 11:23:00
  • Pandas Matplotlib保存图形时坐标轴标签太长导致显示不全问题的解决

    2023-07-22 20:03:09
  • python同时给两个收件人发送邮件的方法

    2021-10-23 07:31:36
  • python 迭代器和iter()函数详解及实例

    2022-03-24 17:55:33
  • 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)

    2022-08-18 14:01:35
  • Django Rest framework解析器和渲染器详解

    2021-06-30 20:46:25
  • js运算精度丢失的2个解决方法

    2024-04-10 10:38:02
  • Python中安装easy_install的方法

    2022-06-22 20:30:00
  • python调用windows api锁定计算机示例

    2021-09-08 03:28:38
  • Oracle系统表外键的更名

    2010-07-26 13:07:00
  • 详解Python使用tensorflow入门指南

    2023-08-22 13:37:09
  • Python使用grequests(gevent+requests)并发发送请求过程解析

    2023-08-14 16:29:01
  • SQL Server各种日期计算方法

    2008-09-11 21:47:00
  • go学习笔记读取consul配置文件详解

    2024-05-09 10:08:14
  • Python中Pyenv virtualenv插件的使用

    2021-10-25 08:07:19
  • 61条面向对象设计的经验原则

    2008-05-08 13:05:00
  • asp之家 网络编程 m.aspxhome.com