Python date operation and wxpython simple interface

        When writing the registration form for processing and analysis, my friend asked me how to count the number of weeks, weeks, and weeks in a period of time in excel. I said it seems to have nothing to do with excel, right? How about I write you a small program, enter the start date and end date separately, and then belong to the day of the week that you want to count, and then come out the statistical results, he said yes.

        Because the date is designed, the datetime package of python can be used to calculate it is much easier than the manual implementation (also divided into leap years).

        First get the keyboard input through input(), and store the start date string in the variable, because the input format is specified in advance (say good to the base friend) as the format of'xxxx.xx.xx', that is, the separator uses dots, so you can Use the following function to convert the string format to datetime format:

start=datetime.datetime.strptime(start_date, '%Y.%m.%d')

        start_time is a string, and'%Y.%m.%d' is the date format of the input string.

        After getting the start date and end date in datetime format, you can directly subtract them to get the time difference in timedelta format, and get the value of int type by calling the days element of the time difference. Since the start and end of the set date are included, the number of days of the int type also needs to be +1 operation:

number_of_day=(end-start).days+1#包括首尾

        The number_of_day obtained in this way is the desired int type of days.

        After that, enter the number of weeks and days of the week you want to count at one time, and use numbers to indicate the number of weeks, separated by spaces or dots (now after thinking about it, it seems that there is no need to do so, and it’s okay to not separate), and split Get a list of multiple single week numbers entered. (It seems that it can be separated without a separator, or it can be matched by a number).

string=input('请一次性用小写数字数字输入想要统计的周几,中间用空格或者点隔开:\n')
string=re.split(' +|\.',string)#'.'转义为'\.' 多个不同长度的分隔符之间用'|'分割开

        After that, the number of weeks corresponding to each number in the string is calculated how many times. First take the integer number of days divided by 7 (number_of_day//7), that is, the number of days included in the calculation of the number of whole weeks, set to n, it can be simplified to n times each week number, and then get the week number of the start date ( start.weekday()+1), i traverses from 1 to the remainder of the number of days divided by 7 (the number of days left in less than a week), starting from the week number of the start date, get the week number of this day ((which_day+i- 1)%7+1)), add it to the map corresponding to the number of weeks, and output it at the end.

    name_map={'1':'Mon','2':'Tue','3':'Wed','4':'Thur','5':'Fri','6':'Sat','7':'Sun'}
    statistics=dict()
    number_of_week=number_of_day//7
    remain_day=number_of_day%7
    for s  in string:
        statistics[name_map[s]]=number_of_week
        
    
    which_day=start.weekday()+1
    for i in range(remain_day):
        name=name_map[str((which_day+i-1)%7+1)]
        if name in statistics:
            statistics[name]+=1
            
    for index in statistics:
        print(index+':'+str(statistics[index]))
        
    xof=input('结束')#防止打包成exe的显示完结果闪退

The complete procedure is as follows:

###project 1
## 

import datetime
import re
while True:
    start_date=input('请输入开始日期:\n')
    if start_date=='0':
        break
    end_date=input('请输入结束日期:\n')
    start=datetime.datetime.strptime(start_date, '%Y.%m.%d')
    end=datetime.datetime.strptime(end_date, '%Y.%m.%d')
    number_of_day=(end-start).days+1#包括首尾
    print('包括开始日期和结束日期一共'+str(number_of_day)+'天\n')
    string=input('请一次性用小写数字数字输入想要统计的周几,中间用空格或者点隔开:\n')
    string=re.split(' +|\.',string)#'.'转义为'\.' 多个不同长度的分隔符之间用'|'分割开
    name_map={'1':'Mon','2':'Tue','3':'Wed','4':'Thur','5':'Fri','6':'Sat','7':'Sun'}
    statistics=dict()
    number_of_week=number_of_day//7
    remain_day=number_of_day%7
    for s  in string:
        statistics[name_map[s]]=number_of_week
        
    
    which_day=start.weekday()+1
    for i in range(remain_day):
        name=name_map[str((which_day+i-1)%7+1)]
        if name in statistics:
            statistics[name]+=1
            
    for index in statistics:
        print(index+':'+str(statistics[index]))
        
    xof=input('结束')

After packaging, as shown in the figure:

But my friend said that the acceptance is unqualified, and the time period cannot be modified. It is troublesome (please, your request does not have this point), but it is a little troublesome to modify the start date and end date under keyboard operation. , So use wx to give him the entire interface!

First create the app and frame:

import wx
app = wx.App()
frame = wx.Frame(None,title = "LCX Project 1",pos = (600,200),size = (500,500))

The frame parameter pos represents the relative position of the upper left corner point on the screen, and size is the frame size. Since repeated runs will show that the app has been created, you need to add the following operations at the beginning:

if 'app' in locals():
    del app

That is, check whether the variable'app' is in the global variable, and if it is, delete it.

Add static text at the specified position of the frame (poss relative to the frame) to display prompts, etc. The display text is label:

start_text=wx.StaticText(frame,label="请输入开始日期:",pos = (20,50))

Add a text box at the specified position of the frame for the user to input text, and size is the size of the text box:

start_date = wx.TextCtrl(frame,pos = (120,45),size = (200,30))

Add a button in the specified position of the frame to call a custom function:

change_start = wx.Button(frame,label = "修改",pos = (350,45),size = (50,30))

And bind the button click function to the custom function change_start_f:

def change_start_f():
    ...
...
change_start.Bind(wx.EVT_BUTTON,change_start_f)

In this way, when the change_start button of the graphical interface is clicked, the change_start_f function will be executed.

Change the week number entered by keyboard to a multi-select box, generate 7 multi-select boxes and put them in the list checkbox, name_map stores the week number corresponding to each number.

name_map={'1':'Mon','2':'Tue','3':'Wed','4':'Thur','5':'Fri','6':'Sat','7':'Sun'}
checkbox=list()
for i in range(7):
    checkbox.append(wx.CheckBox(frame, -1, name_map[str(round(i+1))], pos=(30+50*i, 300)))

Add two lines of code at the end to ensure the display and operation of the interface.

frame.Show()
app.MainLoop()

The click function of the button to the right of the bound start date and end date is as follows:

def change_start_f(event):
    global start
    start=datetime.datetime.strptime(start_date.GetValue(), '%Y.%m.%d')
    show_start=wx.StaticText(frame,label=str(start),pos = (120,95))
def change_end_f(event):
    global end
    end=datetime.datetime.strptime(end_date.GetValue(), '%Y.%m.%d')
    show_end=wx.StaticText(frame,label=str(end),pos = (120,195))

global x: Set x as a global variable, which can be accessed by other functions. After clicking the "modify" button, the input value of the text box start_date (obtained by start_date.GetValue()) will be written into the global variable start, and in the text box The bottom shows start, end is the same.

Click "Display Results" to call the function:

def show_ans(event):
    number_of_day=(end-start).days+1
    string='包括开始日期和结束日期一共'+str(number_of_day)+'天\n'
    show_days=wx.StaticText(frame,label=string,pos = (20,400))
    statistics=dict()
    number_of_week=number_of_day//7
    remain_day=number_of_day%7
    for check in checkbox:
        if check.GetValue():
            statistics[check.GetLabel()]=number_of_week
    which_day=start.weekday()+1
    for i in range(remain_day):
        name=name_map[str((which_day+i-1)%7+1)]
        if name in statistics:
            statistics[name]+=1
    i=0     
    frame2=wx.Frame(None,title = "Ans",pos = (600,200),size = (200,300))
    for index in statistics: 
        string=index+':'+str(statistics[index])
        wx.StaticText(frame2,label=string,pos = (20,30*i))
        i+=1
    frame2.Show()

Most of the content is similar to the content of the previous black box version, in which the GetValue() function of each check in the call list checkbox is traversed to determine whether the corresponding check box is ticked (if it is ticked, it will return True, otherwise False). The display name of the checked box (check.GetLabel()) is placed in the staticstics dictionary and mapped to the complete week number. The other operations are the same.

Because the previous results are still on the frame when executed multiple times (for example, when Monday, Tuesday, Wednesday is displayed for the first time, and Thursday and Friday are displayed for the second time, Monday and Tuesday will be overwritten by the second time, but not overwritten. It will continue to be displayed on Wednesday), so just create a frame2 and display the result in it! Hahaha, how inexplicable.

Complete code:

# -*- coding: utf-8 -*-
"""
Created on Mon Nov 18 14:50:29 2019

@author: 71405
"""
import datetime
if 'app' in locals():
    del app
import wx
def change_start_f(event):
    global start
    start=datetime.datetime.strptime(start_date.GetValue(), '%Y.%m.%d')
    show_start=wx.StaticText(frame,label=str(start),pos = (120,95))
def change_end_f(event):
    global end
    end=datetime.datetime.strptime(end_date.GetValue(), '%Y.%m.%d')
    show_end=wx.StaticText(frame,label=str(end),pos = (120,195))
def show_ans(event):
    number_of_day=(end-start).days+1
    string='包括开始日期和结束日期一共'+str(number_of_day)+'天\n'
    show_days=wx.StaticText(frame,label=string,pos = (20,400))
    statistics=dict()
    number_of_week=number_of_day//7
    remain_day=number_of_day%7
    for check in checkbox:
        if check.GetValue():
            statistics[check.GetLabel()]=number_of_week
    which_day=start.weekday()+1
    for i in range(remain_day):
        name=name_map[str((which_day+i-1)%7+1)]
        if name in statistics:
            statistics[name]+=1
    i=0     
    frame2=wx.Frame(None,title = "Ans",pos = (600,200),size = (200,300))
    for index in statistics: 
        string=index+':'+str(statistics[index])
        wx.StaticText(frame2,label=string,pos = (20,30*i))
        i+=1
    frame2.Show()
app = wx.App()
frame = wx.Frame(None,title = "LCX Project 1",pos = (600,200),size = (500,500))
start_text=wx.StaticText(frame,label="请输入开始日期:",pos = (20,50))
start_text=wx.StaticText(frame,label="请输入结束日期:",pos = (20,150))
start_text=wx.StaticText(frame,label="请选择要统计的周几:",pos = (20,250))
start_date = wx.TextCtrl(frame,pos = (120,45),size = (200,30))
end_date = wx.TextCtrl(frame,pos = (120,145),size = (200,30))
change_start = wx.Button(frame,label = "修改",pos = (350,45),size = (50,30))
change_start.Bind(wx.EVT_BUTTON,change_start_f)  
change_end = wx.Button(frame,label = "修改",pos = (350,145),size = (50,30))
change_end.Bind(wx.EVT_BUTTON,change_end_f)   
name_map={'1':'Mon','2':'Tue','3':'Wed','4':'Thur','5':'Fri','6':'Sat','7':'Sun'}
show = wx.Button(frame,label = "显示结果",pos = (150,360),size = (100,30))
show.Bind(wx.EVT_BUTTON,show_ans)  
checkbox=list()
for i in range(7):
    checkbox.append(wx.CheckBox(frame, -1, name_map[str(round(i+1))], pos=(30+50*i, 300)))
    
frame.Show()
app.MainLoop()

Package operation effect:

Guess you like

Origin blog.csdn.net/qq_36614557/article/details/103139685