1.先上界面:
代码:
先做一个基窗口,list和text会用到,直接继承减少代码量
import tkinter
class BaseWindowShow:
def __init__(self):
self.win=tkinter.Tk()#构造窗体
self.win.geometry("900x800+0+0")
def show(self):
self.win.mainloop()#进入消息循环
listshow:
import 数据查询.BaseWindow
import tkinter
class ListShow(数据查询.BaseWindow.BaseWindowShow):#继承BaseWindow
def __init__(self):
数据查询.BaseWindow.BaseWindowShow.__init__(self)
self.mylist=tkinter.Listbox(self.win,width=900,height=800)
self.mylist.pack()
def addata(self,insertstr):#增加数据
self.mylist.insert(tkinter.END,insertstr)
textshow:
import tkinter
import 数据查询.BaseWindow
class TextShow(数据查询.BaseWindow.BaseWindowShow):
def __init__(self):
数据查询.BaseWindow.BaseWindowShow.__init__(self)
self.text=tkinter.Text(self.win,width=900,height=800)
self.text.pack()
def addata(self,insertstr):
self.text.insert(tkinter.END,insertstr)
这两个比较好理解,重要的是搜索和输出界面
搜索:
import 数据查询.ListShow
import 数据查询.TextShow
import codecs
class bigdatafind:
def __init__(self,path,howtoshow):
self.file=codecs.open(path,"rb","GBK","ignore")#打开文件
self.howtoshow=howtoshow
self.showview=None #窗体-创建
if self.howtoshow=="listshow":
self.showview=数据查询.ListShow.ListShow()
elif self.howtoshow=="textshow":
self.showview =数据查询.TextShow.Textshow()
else:
pass
def find(self,searchstr):
while True:
line=self.file.readline()
if line.find(searchstr)!=-1:
print(line,end="") #显示数据
#插入
if self.showview != None:
self.showview.addata(line) # 加数据 多态(通用方法)
if not line: #读不到数据退出
break
def show(self):
if self.showview!=None:
self.showview.show() #显示 多态(通用方法)
def __del__(self):
self.file.close()
输入界面:
import tkinter
from tkinter import ttk
import BigdataFind
import 数据查询.BigdataFind
class Inputview:
def __init__(self):
self.win=tkinter.Tk()
self.win.geometry("800x600+300+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=200,y=0)
self.comvalue=tkinter.StringVar()
self.comboxlist=ttk.Combobox(self.win,textvariable=self.comvalue,width=80)
self.comboxlist["values"]=("listshow", "textshow", "tableshow")
self.comboxlist.current(0)
self.comboxlist.bind("<<ComboboxSelected>>",self.go)
self.comboxlist.place(x=0,y=50)
self.howtoshow="listshow"
self.comvaluefile=tkinter.StringVar()
self.comvaluelistfile=ttk.Combobox(self.win,textvariable=self.comvaluefile,width=80)
self.comvaluelistfile["values"]=(r"D:\桌面\dangdanglite.txt",
r"D:\桌面\csdnsortindex.txt")
self.comvaluelistfile.current(0)
self.comvaluelistfile.bind("<<ComboboxSelected>>",self.filego)
self.comvaluelistfile.place(x=0,y=100)
self.howtoshowfile=r"D:\桌面\dangdanglite.txt"
def go(self,*args):
self.howtoshow=self.comboxlist.get()
print(self.comboxlist.get())
def filego(self,*args):
self.howtoshowfile=self.comvaluelistfile.get()
print(self.comvaluelistfile.get())
def show(self):
self.win.mainloop()
def search(self):
print("start",self.entry.get())
big=数据查询.BigdataFind.bigdatafind(self.howtoshowfile,self.howtoshow)
big.find(self.entry.get())
big.show()
由于表格有点繁琐偷了个懒,没做。
最后就是最简单的输入了:
import 数据查询.InputView
inputs=数据查询.InputView.Inputview()
inputs.show()
在此处运行就可以进行查询