Python 简易版小工具 | 计算天数

简易版小工具 | 计算天数


需求


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

实现思路


  1. 使用 tkintertkinter.ttk 对界面进行布置;
  2. 使用 calendar 计算天数;
  3. 规范输入日期的格式;
  4. 对月份,天数进行逻辑判断;
  5. 输入错误抛出异常提示。

代码实现


# -*- coding: utf-8 -*-
'''
@File: calc_day_v2.py
@Time: 2020/02/12 20:33:22
@Author: 大梦三千秋
@Contact: [email protected]
'''

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

使用效果


正确输入的效果如下:

cacl_day_true.jpg

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

cacl_day_wrong_format.jpg

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

cacl_day_wront_month.jpg

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

cacl_day_wrong_day.jpg


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


欢迎搜索关注微信公众号《书所集录》

发布了82 篇原创文章 · 获赞 43 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45642918/article/details/104286053
今日推荐