升级为带有选择框
分三种查看格式一种是表格查看 一种是文本查看 一种是列表
1.列表查看类
# listbox 显示数据 import tkinter class ListShowData: def __init__(self): self.win=tkinter.Tk() # 构造窗体 self.mylist=tkinter.Listbox(self.win,width=100) #列表框 self.mylist.pack() def adddata(self,insertstr): self.mylist.insert(tkinter.END,insertstr) def show(self): self.win.mainloop() # 进入消息循环 if __name__ == '__main__': listshow = ListShowData() listshow.adddata(123) listshow.adddata(123) listshow.adddata(123) listshow.show()
2.文本查看类
# text文件模式显示数据 import tkinter class TextShowData: def __init__(self): self.win = tkinter.Tk() # 构造窗体 self.text = tkinter.Text(self.win, width=100) # 列表框 self.text.pack() def adddata(self, insertstr): self.text.insert(tkinter.INSERT, insertstr) def show(self): self.win.mainloop() # 进入消息循环 if __name__ == '__main__': listshow =TextShowData() listshow.adddata("123\r\n") listshow.adddata("123\r\n") listshow.adddata("123\r\n") listshow.adddata("123\r\n") listshow.show()
3.表格查看
# tree表格显示数据 import tkinter from tkinter import ttk class TableShowData(): def __init__(self): self.win=tkinter.Tk() self.win.geometry("1000x1000+100+100") self.idnum = 0 # 表示插入的行数 self.tree=ttk.Treeview(self.win,height=500) # 表格 self.tree["column"]=("用户","密码","邮箱") self.tree.column("用户",width=200) # 表示列,不显示 self.tree.column("密码",width=200) self.tree.column("邮箱",width=200) self.tree.heading("用户",text="用户") self.tree.heading("密码",text="密码") self.tree.heading("邮箱",text="邮箱") self.tree.pack() def adddata(self,insertstr): # 增加数据 datalist=insertstr.split(" # ") if len(datalist) == 3: self.tree.insert("",self.idnum,text=(self.idnum+1),values=(datalist[0], datalist[1], datalist[2])) self.idnum+=1 def show(self): self.win.mainloop() if __name__ == '__main__': mytable=TableShowData() mytable.adddata("19810831 # 273018 # wangweirun@sina.com") mytable.adddata("19811103 # 74215 # luckyydd@263.net") mytable.adddata("198212160123 # 433318 # xpj-1216@163 com") mytable.show()
4.数据查询类
import codecs import python基础.day16.图形化编程.listshow import python基础.day16.图形化编程.textshow import python基础.day16.图形化编程.tableshow 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=python基础.day16.图形化编程.listshow.ListShowData() elif self.howtoshow=="textshow": self.showview = python基础.day16.图形化编程.textshow.TextShowData() elif self.howtoshow=="tableshow": self.showview = python基础.day16.图形化编程.tableshow.TableShowData() def find(self,searchstr): while True: line = self.file.readline() if line.find(searchstr) != -1: print(line,end="") # 显示数据 # 插入 if self.showview != None: # 显示 self.showview.adddata(line) if not line: # 找不到数据 break def show(self): if self.showview != None: # 显示 self.showview.show() # 显示查找到的内容 def __del__(self): self.file.close() if __name__ == '__main__': bigfind = BigDataFind(路径,使用那个方法显示) bigfind.find(查找内容) bigfind.show()
5.输入窗口
import tkinter from tkinter import ttk from python基础.day16.图形化编程.bigdatafind import BigDataFind class InputViem(): def __init__(self): self.win =tkinter.Tk() self.win.geometry("900x300+0+0") # 输入框 self.entry=tkinter.Entry(self.win) #input self.entry.place(x=0,y=0) # 按钮 self.button=tkinter.Button(self.win,text="搜索",command=self.search) self.button.place(x=150,y=0) # 类型组合框1 self.comvalue=tkinter.StringVar() #窗体的自带文本 新建一个值 self.comboxlist=ttk.Combobox(self.win, textvariable=self.comvalue) self.comboxlist["values"]=("listshow","tableshow","textshow") self.comboxlist.current(0) #默认显示第一个 self.comboxlist.bind("<<ComboboxSelected>>", self.go) # 绑定组合框事件 self.comboxlist.place(x=0,y=100) # 文件组合框2 self.comvalueFile = tkinter.StringVar() # 窗体的自带文本 新建一个值 self.comboxlistFile = ttk.Combobox(self.win, textvariable=self.comvalueFile, width=90) self.comboxlistFile["values"] = (r"G:\我的python\python基础\大数据相关数据\csdn.txt", r"G:\我的python\python基础\大数据相关数据\QQ.txt", r"G:\我的python\python基础\大数据相关数据\kaifang.txt" ) self.comboxlistFile.current(0) self.comboxlistFile.bind("<<ComboboxSelected>>", self.goFile) # 绑定组合框事件 self.comboxlistFile.place(x=0, y=200) def go(self,*tuple): print(self.comboxlist.get()) def goFile(self,*tuple): print(self.comboxlistFile.get()) def show(self): self.win.mainloop() def search(self): print(self.entry.get()) print(self.comboxlist.get()) bigfind = BigDataFind(self.comboxlistFile.get(), self.comboxlist.get()) bigfind.find(self.entry.get()) bigfind.show() if __name__ == '__main__': win=InputViem() win.show()