简化布局代码
import time
try:
from Tkinter import * #python 2.x
from tkFileDialog import * #python 2.x
except importError as e:
from tkinter import *
from tkinter.filedialog import *
import binascii
import os
import os.path
import re
class TextToHex:
def __init__(self):
root = Tk()
root.title("Find Head&Rear")
# frame1 排版布局
frame1 = frame(root,pady = 6)
frame1.grid(row=1, column=1)
# 文件夹路径 账单头标识符 头-文件总数 等项布局
lablenames = ['文件夹路径:','账单头标识符:','头-文件总数:','账单尾标识符:','尾-文件总数:','尾-文件总数:','账单尾标识符:']
self.var1 = StringVar()
self.var2 = StringVar()
self.var4 = StringVar()
self.var3 = StringVar()
self.var5 = StringVar()
EntryVar = [self.var1, self.var2, self.var4,self.var5,self.var3]
for colone in range(3):
coloneLable = Label(frame1,text = lablenames[colone])
coloneLable.grid(row=1+colone, column=1, sticky=W)
coltoEntry = Entry(frame1, textvariable=EntryVar[colone])
coltoEntry.grid(row=1+colone, column=2, padx=5, pady=2)
for coltwo in range(2):
coltwolable = Label(frame1, text=lablenames[-1-coltwo])
coltwolable.grid(row=2+coltwo, column=3, sticky=W)
coltwoentry = Entry(frame1,textvariable = EntryVar[-1-coltwo])
coltwoentry.grid(row=2+coltwo, column=4, padx=5)
# 三个按钮布局
ButtonList = ['选择文件夹','查询','导出结果']
deflist = [self.DirSelect,self.SearchHR,self.SaveFile]
for butrowone in range(3):
if butrowone == 0:
buttons = Button(frame1, text=ButtonList[butrowone], command=deflist[butrowone],padx=20)
buttons.grid(row=1, column=3,sticky=W)
else:
buttons = Button(frame1, text=ButtonList[butrowone], command=deflist[butrowone],padx=(20 if butrowone==1 else 0 ))
buttons.grid(row=1, column=4,padx=5,sticky=('W' if butrowone==1 else 'E'))
# frame2 排版布局
frame2 = frame(root,pady = 5,padx = 5)
frame2.grid(row=2, column=1)
scrollbar = Scrollbar(frame2)
scrollbar.pack(side=RIGHT, fill=Y)
self.ListBox = Listbox(frame2, width=80, height=30, yscrollcommand=scrollbar.set)
self.ListBox.pack()
scrollbar.config(command=self.ListBox.yview)
root.mainloop()
# 选择账单文件夹路径
def DirSelect(self):
self.DirSelectPath = askdirectory()
self.var1.set(self.DirSelectPath.replace("/","\"))
# 在账单中查找头尾的数量,判断头尾的唯一性
def SearchHR(self):
# openread = open('11.spc.bak','rb') # 16进制字符串文件
# readf = openread.read()
# readf1 = readf.decode().replace(' ','') # 去空格,字符串简单的替换速度快于re.sub
# #readf1 = re.sub(' ','',readf.decode())
# #print(readf1)
#
# readf1_b2a = binascii.unhexlify(readf1.encode()) # bin to ascii
# print(readf1_b2a)
THFC = 0
TRFC = 0
m = 0
self.list = []
self.ListBox.delete(0,END)
if os.path.isdir(self.var1.get()):
for root,dirs,files in os.walk(self.var1.get()):
for self.name in files:
''' -- endswith(suffix,start,end),suffix(后缀)可以是一个字符/字符串/元组,判断文件后缀,筛选出符合条件的文件
添加时间 -- 2020/10/18
'''
if self.name.endswith(('.txt','.spc')):
i = 1
m += i
#print(self.name)
Fname = os.path.join(root,self.name)
self.list.append(Fname)
#print(Fname)
OpenFile = open(Fname,"rb") # 图片账单文件比较大,必须使用rb格式打开,否则会丢失数据
ReadFile = OpenFile.read()
# python3.x严格要求格式,必须转成字符串类型.decode()
# binascii.hexlify() 数据转换成16进制效率高
# ' '.join(re.findall('..')) 16进制数据转成字符串后,设置每两个字节中间加一个空格,防止查找时匹配有问题(30A10A0A0A0A,如不加空格会取到3 0A)
TxtToHex = ' '.join(re.findall('..',binascii.hexlify(ReadFile).decode()))
#print(TxtToHex)
if self.var2.get() != "":
HFC = len(re.findall(' '.join(re.findall('..',self.var2.get().replace(' ',''))),TxtToHex,flags = re.I))
else:
HFC = 0
if self.var3.get() != "":
RFC = len(re.findall(' '.join(re.findall('..',self.var3.get().replace(' ',''))),TxtToHex,flags = re.I))
else:
RFC = 0
if HFC == RFC:
self.ListBox.insert(END,"文件名:" + self.name + "|" + "头个数:" + str(HFC) + "|" + "尾个数:" + str(RFC) + "n")
else:
self.ListBox.insert(END, "文件名:" + self.name + "|" + "头个数:" + str(HFC) + "|" + "尾个数:" + str(RFC) + "n")
self.ListBox.itemconfig(m-1,fg="red") #设置当前行的字体颜色
if HFC > 0:
THFC += i
if RFC > 0:
TRFC += i
OpenFile.close()
self.ListBox.insert(0,"文件总数:" + str(m) + "n")
self.ListBox.itemconfig(0, fg="blue")
self.var4.set(str(THFC))
self.var5.set(str(TRFC))
else:
self.ListBox.insert(END,"请选择正确的文件夹路径!!!" + "n")
self.ListBox.itemconfig(0,fg="red")
# 下期准备做记录中打开选择的文件
# def WatchFile(self):
# try:
# print(self.list)
# except:
# self.ListBox.insert(END,"无有效文件,打不开" + "n")
# 导出所有的查找记录
def SaveFile(self):
SaveFileName = asksaveasfilename(title='导出',defaultextension = '.xls')
if SaveFileName != "":
OpenSaveFile = open(SaveFileName,'w')
for i in range(len(self.ListBox.get(0,END))):
OpenSaveFile.write(self.ListBox.get(0,END)[i])
OpenSaveFile.close()
TextToHex()