实现画图,数据筛选,保存
import shutil
from tkinter import *
import tkinter
from tkinter.filedialog import askdirectory
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from matplotlib.backend_bases import key_press_handler
import pandas as pd
import datetime
from get_file_list import get_one_type_file_list
import time
import stat
import os
class As:
"""
使用StringVar() 和 textvariable
对Button进行绑定
实现Button对数据进行操作
解决Button传参问题
StringVar()的数需要使用.get()获取值
"""
def __init__(self, master):
self.root = Frame(master)
self.num1 = StringVar() # 第一个数字
self.num2 = StringVar()
self.num3 = StringVar()
self.createpage()
def ii(self):
sys.exit(0)
# 绑定上面定义的键盘事件处理函数
def _quit(self):
"""点击退出按钮时调用这个函数"""
root.quit() # 结束主循环
root.destroy() # 销毁窗口
def selectPath(self):
path_ = askdirectory()
path.set(path_)
def createpage(self):
self.root.pack()
Label(self.root, text='起时间').grid(row=0, column=0, stick=W, pady=10)
# textvariable和StringVar的num1绑定
Entry(self.root, textvariable=self.num1).grid(row=0, column=1, stick=E)
Label(self.root, text='停时间').grid(row=0, column=10, stick=W, pady=10)
# textvariable和StringVar的num2绑定
Entry(self.root, textvariable=self.num2).grid(row=0, column=11, stick=E)
# Button传递参数
Label(self.root, text='文件名').grid(row=0, column=20, stick=W, pady=10)
# textvariable和StringVar的num1绑定
Entry(self.root, textvariable=self.num3).grid(row=0, column=21, stick=E)
Button(self.root, text='输入', command=self.btn_def).grid(row=1, column=10, stick=W)
Button(self.root, text='NEXT', command=self._quit).grid(row=1, column=20, stick=W)
Button(self.root, text='关闭', command=self.ii).grid(row=1, column=0, stick=W)
# Label(self.root, text="目标路径:").grid(row=0, column=0)
# Entry(self.root, textvariable=path).grid(row=0, column=1)
def btn_def(self):
# 使用.get()获取值
#num = int(self.num1.get()) + int(self.num2.get())
ts_st=self.num1.get()
ts_ed=self.num2.get()
mingzi=self.num3.get()
b=TS[0].strftime('%Y-%m-%d ')+ts_ed
a=(TS[0].strftime('%Y-%m-%d ')+ts_st)
starttime= int(time.mktime(datetime.datetime.strptime(a, '%Y-%m-%d %H:%M:%S').timetuple()))
endtime= int(time.mktime(datetime.datetime.strptime(b, '%Y-%m-%d %H:%M:%S').timetuple()))
#stt = datetime.datetime.strptime(mach['ts'], '%Y-%m-%d %H:%M:%S+0000')
for var,mach in enumerate(df1['ts_date']):
# c= int(time.mktime(mach.timetuple()))
if (mach - starttime) == 0:
cd=var
if (mach - endtime) == 0:
cq=var
del(df1['ts_date'])
df2=df1.loc[cd:cq]
df2.to_csv(filePath+'\\'+mingzi+'.csv',index=False)
print ('已经存好文件了!')
if __name__ == '__main__':
strInputDir = (r"C:\Users\Administrator\Desktop\appliance")
strFileSuffix = "csv"
strOutputDir = ".\\evts"
filePath = r'C:\Users\Administrator\Desktop\save_data' # 文件路径
if os.path.exists(filePath):
for fileList in os.walk(filePath):
for name in fileList[2]:
os.chmod(os.path.join(fileList[0], name),stat.S_IWRITE)
os.remove(os.path.join(fileList[0], name))
shutil.rmtree(filePath)
os.mkdir(filePath)
lstFilePath, lstFileName = get_one_type_file_list(strInputDir, strFileSuffix)
for i in lstFilePath:
root = tkinter.Tk() # 创建tkinter的主窗口r
root.title("呆瓜出品")
path = StringVar()
f = Figure(figsize=(10, 6), dpi=100)
a = f.add_subplot(111) # 添加子图:1行1列第1个
df1 = pd.read_csv(i)
print(i)
TS, t = [], []
for row in df1['ts']:
ts = datetime.datetime.strptime(row, '%Y-%m-%d %H:%M:%S+0800')
TS.append(ts)
a.plot(TS, df1['w'], 'black')
a.plot(TS, df1['var'], 'red')
df1['ts_date'] = df1['ts'].map(lambda x: int(time.mktime(datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S+0800').timetuple())))
# # 生成用于绘sin图的数据
# # 将绘制的图形显示到tkinter:创建属于root的canvas画布,并将图f置于画布上
canvas = FigureCanvasTkAgg(f, master=root)
canvas.draw() # 注意show方法已经过时了,这里改用draw
canvas.get_tk_widget().pack(side=tkinter.TOP, # 上对齐
fill=tkinter.BOTH, # 填充方式
expand=tkinter.YES) # 随窗口大小调整而调整
# matplotlib的导航工具栏显示上来(默认是不会显示它的)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=tkinter.TOP, # get_tk_widget()得到的就是_tkcanvas
fill=tkinter.BOTH,
expand=tkinter.YES)
As(root)
root.mainloop()
将数据画图,可以截取时间,回调数据,并保存成csv