用继承的方法改造数据查询的界面

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()

在此处运行就可以进行查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值