最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python制作簡(jiǎn)易版小工具之計(jì)算天數(shù)的實(shí)現(xiàn)思路

 更新時(shí)間:2020年02月13日 10:34:01   作者:大夢(mèng)三千秋  
這篇文章主要介紹了Python制作簡(jiǎn)易版小工具之計(jì)算天數(shù)的實(shí)現(xiàn)思路,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

需求

給定一個(gè)日期,格式如 “2020-2-12”,計(jì)算出這個(gè)日期是 2020 年的第幾天?

實(shí)現(xiàn)思路

  1. 使用 tkinter 和 tkinter.ttk 對(duì)界面進(jìn)行布置;
  2. 使用 calendar 計(jì)算天數(shù);
  3. 規(guī)范輸入日期的格式;
  4. 對(duì)月份,天數(shù)進(jìn)行邏輯判斷;
  5. 輸入錯(cuò)誤拋出異常提示。

代碼實(shí)現(xiàn)

# -*- coding: utf-8 -*-
'''
@File: calc_day_v2.py
@Time: 2020/02/12 20:33:22
@Author: 大夢(mèng)三千秋
@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):
  '''自定義異常類(lèi)
  '''
  def __init__(self, message):
    self.message = message
def calculate(*args):
  '''計(jì)算天數(shù)方法
  '''
  try:
    # 用以存儲(chǔ)天數(shù)
    nums = 0
    # 獲取輸入框中的數(shù)據(jù)
    year, month, day = [int(elem) for elem in date.get().split('-')]
    # 判斷月份,規(guī)定在 1-12 之間
    if 1 <= month <= 12:
      # 遍歷計(jì)算天數(shù)
      for month_x in range(1, month + 1):
        # 計(jì)算每月的天數(shù)
        _, month_day = calendar.monthrange(year, month_x)
        # 遍歷的月份等于當(dāng)前月份,對(duì)天數(shù)進(jìn)行規(guī)整
        if month_x == month:
          # 文本輸入框給出的天數(shù)不符合,則拋出異常
          if day > month_day:
            raise MyException("信息輸入錯(cuò)誤,注意天數(shù)!")
          continue
        nums += month_day
      nums += day
      # 設(shè)置值到文本框
      days.set(nums)
      the_year.set(year)
    else: # 月份超出范圍拋出異常
      raise MyException("信息輸入錯(cuò)誤,注意月份!")
  except MyException as e:
    messagebox.showinfo(title="輸入信息錯(cuò)誤", message=e)
  except Exception as e:
    # print(e)
    messagebox.showinfo(title="輸入信息錯(cuò)誤", message="輸出格式錯(cuò)誤,按照 2020-2-12 這樣的格式輸入。注意月份,天數(shù)!")
root = Tk()
root.title("計(jì)算天數(shù)")
# 設(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))
# 標(biāo)簽及按鈕的布置
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)
# 設(shè)置內(nèi)邊距
for child in mainframe.winfo_children():
  child.grid_configure(padx=5, pady=5)
date_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()

使用效果

正確輸入的效果如下:

 

未按照格式輸入,錯(cuò)誤提示效果:

 

月份輸入錯(cuò)誤,提示效果如下:

 

天數(shù)超出當(dāng)月范圍的錯(cuò)誤提示效果:

 

本篇的內(nèi)容主要是對(duì)昨天的 tkinter 模塊的延展使用,實(shí)現(xiàn)一個(gè)計(jì)算天數(shù)的小工具。

以上所述是小編給大家介紹的Python制作簡(jiǎn)易版小工具之計(jì)算天數(shù)的實(shí)現(xiàn)思路,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論

晋州市| 阿拉善左旗| 阳曲县| 突泉县| 宜黄县| 石嘴山市| 固原市| 武邑县| 呼伦贝尔市| 库伦旗| 南阳市| 辰溪县| 德昌县| 通山县| 新建县| 凌源市| 永和县| 宁安市| 沙雅县| 禹州市| 包头市| 江源县| 志丹县| 察雅县| 昂仁县| 余江县| 宜良县| 同德县| 珠海市| 汾阳市| 曲水县| 本溪| 牙克石市| 寿阳县| 武穴市| 宜兰县| 湘潭县| 盐津县| 北碚区| 武夷山市| 青海省|