记第一次用python写界面
花了两三个小时学了Tkinter,做了一个将数据绘制成图的小工具。
1. 获取路径下的所有文件or获取路径下指定名称的文件
1.1 打开文件
//1. 用来放文本框中的文字
filename = StringVar() //2. 新建一个单行文本框 txtDataPath = tkinter.Entry(window,width=60,textvariable=filename).grid(row=0,column = 1) def OpenFile(): file = filedialog.askopenfilename() if(file): filename.set(file)//3. 打开文件后把文件名写在文本框中
1.2 打开文件夹
选数据的时候要打开文件夹,存图的时候也要,所以if...else...
def OpenDirectory(IsOpenOrSave): directory = filedialog.askdirectory() if(directory): if(IsOpenOrSave == 0): filename.set(directory) else: savepath.set(directory)
1.3 获取路径下指定名称的文件
def listSameFile(dir): str = "Whole" for x in os.listdir(dir): path = os.path.join(dir, x) if os.path.isfile(path) and str in os.path.splitext(x)[0]: split_data(path) if os.path.isdir(path): listSameFile(path)
2. 读取数据进行切割
数据生成的图片将用于深度学习,因此越多越好。两张图片的数据有固定的交集。
代码:
def split_data(data,stride, size): length = data.shape[0] start = 0 end = start + size while(start<length-size): print(start) print(end) start+=stride end = start+size if(end>length): end = length start = length-size print(start) print(end)
写的很烂,绞尽脑汁也没有想到可以只打印一次
3. 绘图
绘图时我希望可以直接保存不要画出来,但是
代码必须是先plt.savefig(‘save.jpg’)再plt.show()才能够保存显示的图像
如果先plt.show()再plt.savefig('save.jpg')图像保存的是白色空白图
如果去掉plt.show()只保存图像plt.savefig('save.jpg'),单幅图像可以,如果循环文件夹里的图像,保存的图像会叠加,我白把数据切开了。
加了一个close()后:(忘记哪条数据了,换了条数据。。。
切片+画图:
def split_data(filepath):
data = pd.read_csv(filepath, sep=' ')
size = data.shape[0]
#print(size)
len = int(datalength.get())
stride = int(datastride.get())
start = 0
end = start + len
while(end<size):
plot_data(data,start,end)
start += stride
end = start + len
end = size
start = end-len
plot_data(data,start,end)
def plot_data(data,start,end):
i = pic_index.get()
i += 1
pic_index.set(i)
#print(i)
#print(start)
#print(end)
data_cut = data[start:end]
ax = plt.gca()
ax.yaxis.set_major_locator(MultipleLocator(0.01))
plt.plot(data_cut, color = 'black')
if (IsShowCoordinate.get() == 0):
plt.axis('off')
if (IsSavePic.get() == 1):
plt.savefig(savepath.get() + "/{}.png".format(i))
plt.close()
else:
plt.show()
plt.close()
4. 全代码
import tkinter from tkinter import * import os from tkinter import filedialog import pandas as pd from matplotlib import pyplot as plt from matplotlib.ticker import MultipleLocator LOGO_PATH = "./fish.ico" window = tkinter.Tk() window.title("数据转化为图片") window.iconbitmap(LOGO_PATH) window.geometry("700x200") window.maxsize(1000, 1200) # ---------------------------------- filename = StringVar() datastride = StringVar() datalength = StringVar() savepath = StringVar() lblDataPath = tkinter.Label(window, text="数据路径").grid(row=0,column = 0) lblSavePath = tkinter.Label(window, text="存图路径").grid(row=1,column = 0) lblDataLength = tkinter.Label(window, text="数据长度").grid(row=2,column = 0) lblDataStride = tkinter.Label(window, text="滑动长度").grid(row=3,column = 0) txtDataPath = tkinter.Entry(window,width=60,textvariable=filename).grid(row=0,column = 1) txtSavePath = tkinter.Entry(window,width=60,textvariable = savepath).grid(row=1,column = 1) txtDataLength = tkinter.Entry(window,width=10,textvariable = datalength).grid(row=2,column = 1) txtDataStride = tkinter.Entry(window,width=10,textvariable = datastride).grid(row=3,column = 1) IsShowCoordinate = IntVar() IsSavePic = IntVar() ckbCoordinate = Checkbutton(window, text = "显示坐标", variable = IsShowCoordinate).grid(row=5,column = 3) ckbSavePic= Checkbutton(window, text = "保存图片", variable = IsSavePic).grid(row=6,column = 3) pic_index = IntVar() IsDrawOnce = IntVar() def OpenFile(): IsDrawOnce.set(0) file = filedialog.askopenfilename() if(file): filename.set(file) def OpenDirectory(IsOpenOrSave): directory = filedialog.askdirectory() if(directory): if(IsOpenOrSave == 0): filename.set(directory) else: savepath.set(directory) def split_data(filepath): i = pic_index.get() data = pd.read_csv(filepath, sep=' ') len = int(datalength.get()) stride = int(datastride.get()) start = 0 end = start + len size = data.shape[0] arr = [] while(end<size): i+=1 pic_index.set(i) data_cut = data[start:end] ax = plt.gca() ax.yaxis.set_major_locator(MultipleLocator(0.01)) plt.plot(data_cut,color='black') if(IsShowCoordinate.get() == 0): plt.axis('off') if(IsSavePic.get() ==1): plt.savefig(savepath.get()+"/{}.png".format(i)) plt.close() else: plt.plot(data_cut) plt.show() start+=stride end = start+len def listSameFile(dir): str = "Whole" for x in os.listdir(dir): path = os.path.join(dir, x) if os.path.isfile(path) and str in os.path.splitext(x)[0]: split_data(path) if os.path.isdir(path): listSameFile(path) def drawing(): listSameFile(filename.get()) def draw_one_pic(): split_data(filename.get()) def btn_OpenFile_click(): IsDrawOnce.set(0) OpenFile() def btn_OpenDirectory_click(): IsDrawOnce.set(1) OpenDirectory(0) def btn_SaveDirectory_click(): OpenDirectory(1) def btn_draw_click(): pic_index.set(0) if(IsDrawOnce.get()==0): draw_one_pic() else: drawing() Button(window, text="打开文件", command=btn_OpenFile_click).grid(row=0,column = 4) Button(window, text="打开文件夹", command=btn_OpenDirectory_click).grid(row=0,column = 5) Button(window, text="选择路径", command=btn_SaveDirectory_click).grid(row=1,column = 4) Button(window, text="绘制", command=btn_draw_click).grid(row=5,column = 4) window.mainloop()
写的实在烂,堪堪运行。
想要设置一个变量以命名图片的名字,不会。。。
控件不会靠左。。。(已解决)
新代码更新在 https://gitee.com/yurj0403/line1.git
生成exe后,ico总是会报错,只好把ico去掉了。