Python制作簡易版小工具之計算天數(shù)的實現(xiàn)思路
更新時間:2020年02月13日 10:34:01 作者:大夢三千秋
這篇文章主要介紹了Python制作簡易版小工具之計算天數(shù)的實現(xiàn)思路,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
需求
給定一個日期,格式如 “2020-2-12”,計算出這個日期是 2020 年的第幾天?
實現(xiàn)思路
- 使用 tkinter 和 tkinter.ttk 對界面進行布置;
- 使用 calendar 計算天數(shù);
- 規(guī)范輸入日期的格式;
- 對月份,天數(shù)進行邏輯判斷;
- 輸入錯誤拋出異常提示。
代碼實現(xiàn)
# -*- 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): '''計算天數(shù)方法 ''' try: # 用以存儲天數(shù) nums = 0 # 獲取輸入框中的數(shù)據(jù) year, month, day = [int(elem) for elem in date.get().split('-')] # 判斷月份,規(guī)定在 1-12 之間 if 1 <= month <= 12: # 遍歷計算天數(shù) for month_x in range(1, month + 1): # 計算每月的天數(shù) _, month_day = calendar.monthrange(year, month_x) # 遍歷的月份等于當前月份,對天數(shù)進行規(guī)整 if month_x == month: # 文本輸入框給出的天數(shù)不符合,則拋出異常 if day > month_day: raise MyException("信息輸入錯誤,注意天數(shù)!") 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 這樣的格式輸入。注意月份,天數(shù)!") root = Tk() root.title("計算天數(shù)") # 設置框架 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) # 設置內(nèi)邊距 for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) date_entry.focus() root.bind('<Return>', calculate) root.mainloop()
使用效果
正確輸入的效果如下:
未按照格式輸入,錯誤提示效果:
月份輸入錯誤,提示效果如下:
天數(shù)超出當月范圍的錯誤提示效果:
本篇的內(nèi)容主要是對昨天的 tkinter 模塊的延展使用,實現(xiàn)一個計算天數(shù)的小工具。
以上所述是小編給大家介紹的Python制作簡易版小工具之計算天數(shù)的實現(xiàn)思路,希望對大家有所幫助!
相關文章
python爬蟲入門教程--優(yōu)雅的HTTP庫requests(二)
requests 實現(xiàn)了 HTTP 協(xié)議中絕大部分功能,它提供的功能包括 Keep-Alive、連接池、Cookie持久化、內(nèi)容自動解壓、HTTP代理、SSL認證等很多特性,下面這篇文章主要給大家介紹了python爬蟲入門中關于優(yōu)雅的HTTP庫requests的相關資料,需要的朋友可以參考下。2017-05-05全網(wǎng)最全python庫selenium自動化使用詳細教程
這篇文章主要介紹了python庫selenium自動化使用詳細教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-01-01Spring實戰(zhàn)之使用util:命名空間簡化配置操作示例
這篇文章主要介紹了Spring實戰(zhàn)之使用util:命名空間簡化配置操作,結合實例形式分析了Spring使用util:命名空間簡化配置操作的具體步驟與相關操作注意事項,需要的朋友可以參考下2019-12-12