最近在帮同时制作一款从txt文件提取想要的数据(如参数名,对应数据,对应的公式等)的程序,最先考虑是利用tkinter模块,编写一个类似GUI功能的页面,做到一定程度后,感觉还是做一个web+数据库比较方便,目前正在学习数据pymysql,先把之前编写的GUI程序贴出来,比较初级,因此,发这篇博客的目的就是想保存一下,后期等自己学的更加熟练后,完善这篇博客。(PS:我是一名机械工程师,玩程序纯属个人爱好!)
这段程序主要是应用与汽车自动变速箱制造行业,主要目的是将总线输出的内容导出后,进行二次处理,以方便变速箱在标定阶段的数据比对!
当然这只是一部分,后期还要进行改善,将来会持续更新!
import re
import tkinter as tk
from functools import partial as pto
from tkinter import Tk, Button,Y
import os,json
import tkinter.filedialog
from tkinter import messagebox
from tkinter import Tk, Button,Y, Canvas, Frame, Scrollbar, VERTICAL, HORIZONTAL,W,N,E
from tkinter import *
class DCT_PARAMETER(object):
def __init__(self,func1='None'):
self.resource_1=[]
self.resource_2=[]
self.resource_3=[]
self.resource_4=[]
self.resource_5=[]
self.resource_6=[]
self.resource_7=[]
self.resource_8=[]
self.resource_9=[]
self.resource_10=[]
self.resource_11=[]
self.resource_12=[]
self.resource_13=[]
self.resource_14=[]
self.resource_15=[]
self.resource_16=[]
self.resource_17=[]
self.root=tk.Tk()
self.root.title("Text made by Jeb")
self.root.geometry('850x1000')
self.sl=Scrollbar(self.root)
self.sl.pack(side=RIGHT,fill=Y)
self.lb=Listbox(self.root, height=150, width=500, yscrollcommand= self.sl.set,selectmode=SINGLE)
self.lb.bind('',self.save_parameter)
self.frame=Frame(self.root)
self.frame.pack(side=RIGHT,fill=Y)
self.fm2 = tk.Frame(self.root)
self.button1 = tk.Button(self.fm2,text = 'open',command =lambda: (self.func1(),self.inser_listbox(),self.search_FUNC()))
self.button1.pack(side='left')
self.button2 = tk.Button(self.fm2,text = 'save',command = lambda:self.save())
self.button2.pack(side='right')
self.fm2.pack(side='top')
self.lb.pack(side = LEFT, anchor= N)
self.sl['command'] = self.lb.yview
def func1(self):
self.filename= tkinter.filedialog.askopenfilename(filetypes = [(" please open txt file", "*.txt")])
with open(self.filename,'r') as f:
self.lines = f.readlines()
for self.line in self.lines:
if self.line !='\n':
self.resource_1.append(self.line.strip(' '))
self.begin_measurement()
self.begin_character()
self.get_link_map_parameter()
def begin_measurement(self):
for i in range(0,len(self.resource_1)):
self.parameter_1=re.compile(r'/begin MEASUREMENT (.*?)"."',re.I)
self.parameter_2=self.parameter_1.findall(self.resource_1[i])
self.parameter_3=re.compile(r'/begin MEASUREMENT (.*?)""',re.I)
self.parameter_4=self.parameter_3.findall(self.resource_1[i])
self.parameter_5=re.compile(r'/begin MEASUREMENT (.*?)"',re.I)
self.parameter_6=self.parameter_5.findall(self.resource_1[i])
if self.parameter_2:
self.resource_2.append(self.parameter_2)
self.resource_3.append(self.resource_1[i+1])
elif self.parameter_4:
self.resource_2.append(self.parameter_4)
self.resource_3.append(self.resource_1[i+1])
elif self.parameter_6:
self.resource_2.append(self.parameter_6)
self.resource_3.append(self.resource_1[i+1])
for elem in self.resource_3:
a = elem.split(' ')
if (a[0])=='UBYTE':
a[0]='U8'
elif (a[0])=='UWORD':
a[0]='U16'
elif (a[0])=='ULONG':
a[0]='U32'
b=a[0]+' '+a[-2]+' '+a[-1]
self.resource_12.append(b)
self.resource_14.append(a[1])
def begin_character(self):
for i in range(0,len(self.resource_1)):
self.parameter_1=re.compile(r'/begin CHARACTERISTIC (.*?)" . "',re.I)
self.parameter_2=self.parameter_1.findall(self.resource_1[i])
self.parameter_3=re.compile(r'/begin CHARACTERISTIC (.*?)""',re.I)
self.parameter_4=self.parameter_3.findall(self.resource_1[i])
self.parameter_5=re.compile(r'/begin CHARACTERISTIC (.*?)"',re.I)
self.parameter_6=self.parameter_5.findall(self.resource_1[i])
if self.parameter_2:
self.resource_4.append(self.parameter_2)
self.resource_5.append(self.resource_1[i+1])
elif self.parameter_4:
self.resource_4.append(self.parameter_4)
self.resource_5.append(self.resource_1[i+1])
elif self.parameter_6:
self.resource_4.append(self.parameter_6)
self.resource_5.append(self.resource_1[i+1])
for elem in self.resource_5:
a = elem.split(' ')
if (a[0])=='UBYTE':
a[0]='U8'
elif (a[0])=='UWORD':
a[0]='U16'
elif (a[0])=='ULONG':
a[0]='U32'
b=a[0]+' '+a[-2]+' '+a[-1]
self.resource_13.append(b)
def get_link_map_parameter(self):
for i in range(0,len(self.resource_1)):
self.parameter=re.compile(r'LINK_MAP "(.*?)"',re.I)
self.parameter_1=self.parameter.findall(self.resource_1[i])
if self.parameter_1:
self.resource_6.append(self.parameter_1)
self.parameter_2= re.search(r'0x[a-zA-Z0-9]+',self.resource_1[i])
if self.parameter_2:
self.parameter_3=self.parameter_2.group()
self.parameter_4=hex(eval(self.parameter_3))
self.resource_7.append(eval(self.parameter_4))
else :
pass
def search_FUNC(self):
for i in range(0,len(self.resource_1)):
self.parameter_1=re.compile(r'/begin COMPU_METHOD (.*?)"',re.I)
self.parameter_2=self.parameter_1.findall(self.resource_1[i])
if self.parameter_2:
self.resource_15.append(''.join(self.parameter_2))
self.parameter_3=re.compile(r'COEFFS_LINEAR (.*?) (.\d*)',re.I)
self.parameter_4=self.parameter_3.findall(''.join(self.resource_1[(i+1):(i+4)]))
self.parameter_5=re.compile(r'/begin FORMULA "(.*?)"',re.I)
self.parameter_6=self.parameter_5.findall(''.join(self.resource_1[(i+1):(i+4)]).replace('\n',' '))
if self.parameter_4:
self.resource_17.append('LINEAR'+str(self.parameter_4))
elif self.parameter_6:
print(self.parameter_6)
self.parameter_7=len(list(''.join(self.parameter_6)))
self.resource_17.append('FORMULA'+str(self.parameter_7)+';'+str(''.join(self.parameter_6)))
else:
self.resource_17.append('None')
def inser_listbox(self):
for i in range(0,len(self.resource_6)):
self.lb.insert(END,self.resource_6[i])
def save_parameter(self,event):
self.w = event.widget
self.index =self.w.curselection()
self.value = self.w.get(self.index)
for i in range(0,len(self.resource_2)):
flat = lambda L: sum(map(flat,L),[]) if isinstance(L,list) else [L]
ol = flat(self.resource_2[i])
if (''.join(self.value)) in (''.join(ol)):
self.resource_8.append('MEASUREMENT '+''.join(self.resource_6[self.index[-1]])+' '+str(self.resource_7[self.index[-1]])+' '+str(self.resource_12[i]))
if i <=(len(self.resource_14)):
for j in range(0,len(self.resource_15)):
if (''.join(self.resource_14[i])) in (''.join(self.resource_15[j])):
self.resource_8.append(str(self.resource_17[j]))
for j in range(0,len(self.resource_4)):
flat = lambda L: sum(map(flat,L),[]) if isinstance(L,list) else [L]
ll=flat(self.resource_4[j])
if (''.join(self.value)) in (''.join(ll)):
self.resource_8.append('CHARACTER '+''.join(self.resource_6[self.index[-1]])+' '+str(self.resource_7[self.index[-1]])+' '+str(self.resource_13[j]))
def save(self):
with open('output.txt',W) as f:
for i in range(0,len(self.resource_8)):
f.write(str(self.resource_8[i]))
f.close()
def main():
d=DCT_PARAMETER()
mainloop()
if __name__=='__main__':
main()