界面实现的效果
先看最后完成的效果:搜索框里可以搜索我们需要的内容,第二行选择我们的文件,第三行是我们需要输出的格式,列表、表格或者文本
先做一个界面
做这个的目的是后面可以继承,在这个界面的基础上进行添加
import tkinter
class BaseWindowShow:
def __init__(self):
self.win=tkinter.Tk()
self.win.geometry("900x800+0+0")
def show(self):
self.win.mainloop()
列表的接口
我们下拉框里需要三个格式之一的列表,先继承上一个界面,对父类进行初始化,没有会报错,再加上一个Listbox列表框,设置大小,再加上右侧滚动条的显示但不能动,这时候再加上滚动链条使得可以上下滚动,再设置到右侧,设置前40个可见其实我也不清楚因为打出来的效果并不是前40个可见,可以多也可以少,删掉也没问题。
加入数据adddata中tkinter.END意味着从最后加入
import BigData.OutputView
import tkinter
class OutputViewList(BigData.OutputView.BaseWindowShow):#继承
def __init__(self,**arg):
super().__init__() #父类初始化
self.mylist=tkinter.Listbox(self.win,width=900,height=800)
self.scrolly=tkinter.Scrollbar(self.win)#滚动条
self.mylist["yscrollcommand"]= self.scrolly.set#list滚动链接
self.scrolly.pack(side=tkinter.RIGHT,fill=tkinter.Y)
self.mylist.see(40) #list只有前40个属于可见
self.mylist.pack()
def adddata(self,data):
self.mylist.insert(tkinter.END,data)
'''
myout=OutputViewList()
for i in range(200):
myout.adddata(str(i))
myout.show()
'''
表格形式的接口
表格的话首先一点就是要分割,先设置一个表格,分割出来的每一类数据都得有一个表头,循环写入表头放在表格里头,其中设置每个表头的长度、名称,设置一个属性splistr,按照什么一个格式进行切割,保存表头这个列表,然后self.table.pack里可以填充,什么意思呢,就是说对X填充那么会发现数据是一整行的,数据显示出来的框框也是围绕着X轴包裹着像一个矮胖的框,对Y轴填充就会很明显数据里的框框像一个瘦高个子的框,那么选择BOTH就会X、Y轴全部包裹的很好。
加入数据,第一点就是切割把数据变成元组的形式,给StringVar变量的赋值使用的是Tuples类型,所以都得用元组的形式,如果切割出来的长度跟给出来的表头一样长,说明是我们需要的数据这也是数据清洗的一部分,那么我们就把数据插入到表格里,按照def insert(self, parent, index, iid=None, **kw):这样的一个部分写入数据
import BigData.OutputView
import tkinter
import tkinter.ttk
class OutputViewTable( BigData.OutputView.BaseWindowShow):
def __init__(self,tableheadlist,spitstr):
super().__init__()
self.table = tkinter.ttk.Treeview(self.win, height=900) # 表格
self.num = 0 # 起始位置
self.table["columns"] = tuple(tableheadlist)#设置表头
for data in tuple(tableheadlist):#循环,设置表头
self.table.column(data, width=100)
self.table.heading(data, text=data) # 表头
self.splitstr=spitstr #按照什么东西切割
self.tableheadlist=tableheadlist #保存列表
self.table.pack(fill=tkinter.BOTH) #x,填充x,y填充y,both全部填充
pass
def adddata(self,data):
lineist = tuple(data.split(self.splitstr)) # 切割并转化为元组
if len(lineist) == len(self.tableheadlist):
self.table.insert("", self.num, text="line" + str(self.num), values=lineist)
self.num += 1
'''
csdn=OutputViewTable(["user","password","email"]," # ")
csdn.adddata("Fengzhongke # 04914 # srzhaoxin@yahoo.com.cn")
csdn.adddata("sgry # 263021 # sgry@371.net")
csdn.adddata("johnhawke # 39380 # heke333@163.net")
csdn.show()
'''
文本的接口
显示对界面的继承,加入一个文本框,从最后插入数据
import BigData.OutputView
import tkinter
class OutputViewText(BigData.OutputView.BaseWindowShow):
def __init__(self,**arg):
super().__init__()
self.text=tkinter.Text(self.win,width=900,height=800)
self.text.pack()
def adddata(self,data):
self.text.insert(tkinter.INSERT,data)
搜索的接口
搜索里有两个模式,第一个简单工厂模式,我们先把需要的文件打开,读入,因为文件格式不对努力白费,所以需要gbk格式解码,并且有打开就有关闭文件也顺手写好。我们有三种格式,那么就if选择语句进行选择需要哪一种格式,然后就是find类,循环查找我们输入的findstr(输入的数据)并且显示出来。
工厂模式,动态的处理问题,看一眼代码对比发现if的选择没有了,在showtype选择的时候我们动态加入了arg,最下面我们可以看到我们showtype选择了表格形式,后面还有tableheadlist=headlist,spitstr=splitstr,什么意思呢,就是arg是对多个keyword进行传参,我预先也不知道有多少个参数,用到了那两个参数,这个意思。
import OutputView
import OutputViewList
import OutputViewTable
import OutputViewText
class BigDataSearch:
'''
def __init__(self,filepath,fileheadlist,linesplitstr,endecode,showtype):#初始化
self.file=open(filepath,"rb") #打开文件
self.endecode=endecode #按照什么编码解码
#简单工厂模式
if showtype=="列表":
self.showwindow=OutputViewList.OutputViewList()#列表
elif showtype=="文本":
self.showwindow=OutputViewText.OutputViewText()#文本
else:
self.showwindow=OutputViewTable.OutputViewTable(fileheadlist,linesplitstr)#表格
def __init__(self,filepath,fileheadlist,linesplitstr,endecode,showtype):#初始化
self.file=open(filepath,"rb") #打开文件
self.endecode=endecode #按照什么编码解码
#工厂模式
self.showwindow=showtype() #通用
'''
def __init__(self,filepath,endecode,showtype,**arg):#初始化
self.file=open(filepath,"rb") #打开文件
self.endecode=endecode #按照什么编码解码
#工厂模式
self.showwindow=showtype(**arg) #通用
def find(self,findstr):#findstr是要查找的数据
while True:
line=self.file.readline()#读取一行数据
if not line:
break
line=line.decode(self.endecode,errors="ignore")#解码
if line.find(findstr)!=-1:
print(line)#找到的数据
self.showwindow.adddata(line)#插入数据,
self.showwindow.show()#显示
def __del__(self):
self.file.close() #关闭文件
'''
path="C:\\Users\\Tsinghua-yincheng\\Desktop\\YinchengDay14\\csdn.txt"
splitstr=" # "
headlist=["user","password","email"]
csdn=BigDataSearch(path,"gbk",BigData.OutputViewTable.OutputViewTable,tableheadlist=headlist,spitstr=splitstr)
csdn.find("yincheng")
'''
输入窗口
下面的是我搜到的,关于StringVar,自己的理解就是对写入的内容进行初始化。
StringVar是Tk库内部定义的字符串变量类型,在这里用于管理部件上面的字符;
这个可以跟踪变量值的变化,普通的python变量不能即时地显示在屏幕上面。
StringVar并不是python内建的对象, 而是属于Tkinter下的对象。
在使用界面编程的时候, 有些时候是需要跟踪变量的值的变化,以保证值的变更随时可以显示在界面上。由于python无法做到这一点,所以使用了tcl的相应的对象,也就是StringVar、BooleanVar、DoubleVar、IntVar所需要起到的作用
首先,输入界面看最上面的图我们需要一个输入框跟一个搜索按钮,下方写了StringVar我就很不明白搜了上面一段文字,大概就是让计算机懂我们的文字。然后我们需要两个下拉框一个filelist,一个showtypelist,对于两个列表的值需要初始化(并且是元组的形式),还得默认先选择第一个,下拉框里需要填入我们需要的事件,一个选择文件,一个选择类型,两个下拉框写好位置,接下来的代码就是一些初始化的过程。后面的类还是比较好理解的。
import tkinter
import tkinter.ttk
import OutputViewList
import OutputViewText
import OutputViewtable
import SearchData
class InputView(object):
def __init__(self,filelist,showtypelist,endecode):#初始化
self.win = tkinter.Tk()
self.win.geometry("600x300+0+0")
self.entry = tkinter.Entry(self.win)#输入框
self.entry.place(x=0,y=0)
self.button=tkinter.Button(self.win,text="搜索",command=self.search)
self.button.place(x=150,y=0)
self.filecomvalue = tkinter.StringVar()
self.showtypevalue=tkinter.StringVar()#初始化界面变量
self.comboxfilelist = tkinter.ttk.Combobox(self.win,textvariable=self.filecomvalue,width = 70)
self.comboxshowtypelist = tkinter.ttk.Combobox(self.win, textvariable=self.showtypevalue,width =70)
self.comboxfilelist["value"] = tuple(filelist)#对两个列表进行初始化
self.comboxshowtypelist["value"] = tuple(showtypelist)
self.comboxfilelist.current(0)#默认选择第一个
self.comboxshowtypelist.current(0)
#绑定事件
self.comboxfilelist.bind("<<ComboboxSelected>>",self.selectfile())
self.comboxfilelist.bind("<<ComboboxSelected>>", self.selectshowtype())
self.comboxfilelist.place(x=0, y=50)
self.comboxshowtypelist.place(x=0, y=100)
self.path = filelist[0]
self.shwotype = showtypelist[0]
self.createshowtype=None
self.endecode = endecode#编码
self.gettype()
self.headlist=[]
self.splitstr =""
self.gettabletype()
#self.mydict = arg #保存参数
def gettype(self):
if self.showtype == "列表":
self.createshowtype = OutputViewList.OutputViewList
elif self.showtype =="文本":
self.createshowtype = OutputViewText.OutputViewText
else:
self.createshowtype = OutputViewtable.OutputViewTable
def gettabletype(self):
csdnsplitstr = " # "
csdnheadlist = ["user", "password", "email"]
dangdangstr = ","
dangdangheadlist = ["mail", "name", "addr", "bookid", "booktype", "phone", "mobile", "price"]
if self.filepath.find("csdn")!=-1:
self.headlist = csdnheadlist
self.splitstr = csdnsplitstr
else:
self.headlist = dangdangheadlist
self.splitstr = dangdangstr
def show(self):
self.win.mainloop()
def search(self):
bigfind = SearchData.BigDataSearch(self.filepath,self.endecode,self.createshowtype,tableheadlist=self.headlist,spitstr=self.splitstr)#改成了通用接口
bigfind.find(self.entry.get())#搜索输入的信息
def selectfile(self,*arg):
self.filepath = self.comboxfilelist.get()
self.gettabletype()
def selectshowtype(self,*arg):
self.showtype = self.comboxshowtypelist.get()
self.gettype()
主函数
调用直接的函数并且写入文件地址、以及类型名称(变量)。
import InputWindow
filelist = [r"F:\尹成python1782G\4.尹成22天python进阶\21.图形界面高级实战\csdn.txt",
r"F:\尹成python1782G\4.尹成22天python进阶\21.图形界面高级实战\dangdanglite.txt"]
showlist = ["列表","文本","表格"]
#filelist,showtypelist,endecode,**arg
myinput = InputWindow.InputView(filelist,showlist,"gbk")
myinput.show()