核心功能函数
import datetime
def getday(time,n):
y,m,d = time.split("-")
y,m,d = int(y),int(m),int(d)
n = int(n)
the_date = datetime.datetime(y,m,d)
result_date = the_date + datetime.timedelta(days=n)
time = result_date.strftime('%Y-%m-%d')
return time
print (getday("2021-04-02","10"))
result : 2021-04-12
PyQt使用
基本布局
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets as QtGui
from PyQt5 import QtCore
from PyQt5.QtGui import *
import os
import sys
import time
import datetime
class time_line_(QtGui.QWidget):
def __init__(self):
super(time_line_,self).__init__()
self.setWindowTitle("time_line!")
start_time = QtGui.QLabel(u'开始时间:')
self.start_time= QtGui.QLineEdit()
day_time = QtGui.QLabel(u'间隔日期:')
self.day_time= QtGui.QLineEdit()
self.display_time= QtGui.QTextEdit()
time_get = QtGui.QPushButton(u"得到时间")
time_clear= QtGui.QPushButton(u"清除时间")
laty_1 = QtGui.QHBoxLayout()
laty_1.addWidget(start_time)
laty_1.addWidget(self.start_time)
laty_2= QtGui.QHBoxLayout()
laty_2.addWidget(day_time)
laty_2.addWidget(self.day_time)
laty_3= QtGui.QHBoxLayout()
laty_3.addWidget(self.display_time)
laty_4= QtGui.QHBoxLayout()
laty_4.addWidget(time_get)
laty_4.addWidget(time_clear)
all_lay= QtGui.QVBoxLayout()
all_lay.addLayout(laty_1)
all_lay.addLayout(laty_2)
all_lay.addLayout(laty_3)
all_lay.addLayout(laty_4)
self.setLayout(all_lay)
self.resize(400,400)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
line = time_line_()
line.show()
sys.exit(app.exec_())
功能补充
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets as QtGui
from PyQt5 import QtCore
from PyQt5.QtGui import *
import os
import sys
import time
import datetime
start_file = "start_time.txt"
change_file = "change_time.txt"
class time_line_(QtGui.QWidget):
def __init__(self):
super(time_line_,self).__init__()
self.setWindowTitle("time_line!")
start_time = QtGui.QLabel(u'开始时间:')
self.start_time= QtGui.QLineEdit()
day_time = QtGui.QLabel(u'间隔日期:')
self.day_time= QtGui.QLineEdit()
self.display_time= QtGui.QTextEdit()
time_get = QtGui.QPushButton(u"得到时间")
time_clear= QtGui.QPushButton(u"清除时间")
laty_1 = QtGui.QHBoxLayout()
laty_1.addWidget(start_time)
laty_1.addWidget(self.start_time)
laty_2= QtGui.QHBoxLayout()
laty_2.addWidget(day_time)
laty_2.addWidget(self.day_time)
laty_3= QtGui.QHBoxLayout()
laty_3.addWidget(self.display_time)
laty_4= QtGui.QHBoxLayout()
laty_4.addWidget(time_get)
laty_4.addWidget(time_clear)
all_lay= QtGui.QVBoxLayout()
all_lay.addLayout(laty_1)
all_lay.addLayout(laty_2)
all_lay.addLayout(laty_3)
all_lay.addLayout(laty_4)
self.setLayout(all_lay)
if os.path.exists(start_file) and os.path.exists(change_file):
self.read_txt()
else:
current_time = datetime.datetime.now().strftime('%Y-%m-%d')
self.start_time.setText(current_time)
self.day_time.setText("100")
time_get.clicked.connect(self.get_time)
time_clear.clicked.connect(self.text_clear)
self.resize(400,400)
def get_time(self):
#print (current_time)
start_day = str(self.start_time.text()).split(" ")
change_day = str(self.day_time.text()).split(" ")
#print (change_day)
out_list = ""
for sd in start_day:
for cd in change_day:
date =self.getday(sd,cd)
sd_ = str(datetime.datetime.strptime(sd,"%Y-%m-%d").strftime('%Y-%m-%d'))
out_data = sd_+" "+ cd +"day--->"+date
out_list = out_list + out_data + "\n"
self.display_time.setPlainText(out_list)
def text_clear(self):
self.display_time.clear()
def read_txt(self):
with open(start_file,"r") as f:
start_data = f.readlines()
with open(change_file,"r") as f:
change_data = f.readlines()
start_str =""
for li in start_data:
li = li.replace("\n","")
li = li.replace(" ","")
if li!=start_data[-1]:
start_str = start_str + li + " "
else:
start_str = start_str + li
self.start_time.setText(start_str)
change_str =""
for li in change_data:
li = li.replace("\n","")
li = li.replace(" ","")
if li!=change_data[-1]:
change_str = change_str + li + " "
else:
change_str = change_str + li
self.day_time.setText(change_str)
def getday(self,time,n):
y,m,d = time.split("-")
y,m,d = int(y),int(m),int(d)
n = int(n)
the_date = datetime.datetime(y,m,d)
result_date = the_date + datetime.timedelta(days=n)
time = result_date.strftime('%Y-%m-%d')
return time
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
line = time_line_()
line.show()
sys.exit(app.exec_())
说明
如果存在这两个文件,读取信息加载入界面(类似于配置文件)
如果不存在,获取当天时间加载入界面,间隔日期默认100。
我们自己可以随其添加文本框内容,必须以一个空格间隔。
我们也可以修改两个txt文件信息,必须换行间隔。
点击得到时间会根据两个文本框里的内容,自动计算日期,并写入多行文本框。