demand (economics)
Given a date in the format "2020-2-12", calculate what day of the year 2020 is this date?
Ideas for implementation
- Use tkinter and to lay out the interface;
- Use calendar to calculate the number of days;
- Standardize the format for entering dates;
- Make logical judgments about months, days;
- Input error throws an exception.
code implementation
# -*- coding: utf-8 -*- ''' @File: calc_day_v2.py @Time: 2020/02/12 20:33:22 @Author: A Dream of Three Thousand Years @Contact: yiluolion@ ''' # Put the import lib here from tkinter import * import as messagebox from tkinter import ttk import calendar class MyException(BaseException): ''' Custom Exception Classes ''' def __init__(self, message): = message def calculate(*args): ''' Method of calculating the number of days ''' try: # Used to store days nums = 0 # Get the data in the input box year, month, day = [int(elem) for elem in ().split('-')] # Judgment month, specified between 1-12 if 1 <= month <= 12: # Iterate over the number of days for month_x in range(1, month + 1): # of days in a month calculated _, month_day = (year, month_x) # Iterate over months equal to the current month, regularizing the number of days if month_x == month: # Exception thrown if the number of days given in the text input box does not match if day > month_day: raise MyException("Information entered incorrectly, note the number of days!") continue nums += month_day nums += day # Set values to text boxes (nums) the_year.set(year) else: # Exception thrown for month out of range raise MyException("Wrong information input, pay attention to the month!") except MyException as e: (title="Error entering information.", message=e) except Exception as e: # print(e) (title="Error entering information.", message="The output is formatted incorrectly, enter it in a format like 2020-2-12. Note the month, the number of days!") root = Tk() ("Counting days") # Setting up the frame mainframe = (root, padding="3 3 12 12") (column=0, row=0, sticky=(N, S, E, W)) (0, weight=1) (0, weight=1) date = StringVar() the_year = StringVar() days = StringVar() # Textbox widget layout date_entry = (mainframe, width=10, textvariable=date) date_entry.grid(column=2, row=1, sticky=(W, E)) # Layout of tabs and buttons (mainframe, text="Example: 2020-2-12").grid(column=5, row=1, sticky=(W, E)) (mainframe, textvariable=days).grid(column=4, row=2, sticky=(W, E)) (mainframe, textvariable=the_year).grid(column=2, row=2, sticky=(W, E)) (mainframe, text="Calculate", command=calculate).grid(column=5, row=3) (mainframe, text="Date:").grid(column=1, row=1, sticky=E) (mainframe, text="This day is").grid(column=1, row=2, sticky=E) (mainframe, text="The first of the year.").grid(column=3, row=2, sticky=E) (mainframe, text="Heaven.").grid(column=5, row=2, sticky=W) # Set inner margins for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) date_entry.focus() ('<Return>', calculate) ()
utilization effect
The effect of correct input is as follows:
Failure to enter in format, error message effect:
The month is entered incorrectly and the prompt effect is as follows:
Error alert effect for days out of range for the month:
This post focuses on an extended use of yesterday's tkinter module to implement a widget that calculates the number of days.
The above is a small introduction to Python to create a simple version of the gadget to calculate the number of days of the realization of the idea, I hope to help you!