python自学-class17(down)-使用继承方法改进之前的数据查询

1.BaseWindow
最主要的一步就是将各个类中重合部分提取出来,比如构造窗体,显示窗体,进入消息循环等步骤,然后集中初始化。然后弄清楚继承部分的关系,其他代码绝大部分都没有发生改变。

import tkinter
class BaseWindowShow:
    def __init__(self):
        self.win=tkinter.Tk() #构造窗体
        self.win.geometry("800x800+300+0")   #搜索数据显示窗口
    def show(self):
        self.win.mainloop()

2.Main

import 数据查询.inputview
inputs= 数据查询.inputview.InputView()
inputs.show()

3.inputview


#coding=gbk
import tkinter
from tkinter import ttk
import 数据查询.BigDataFind

class InputView:
    def __init__(self):
        self.win = tkinter.Tk()
        self.win.geometry("800x800+300+0")   #设置界面大小及位置

        self.entry = tkinter.Entry(self.win)  #导入文本框
        self.entry.place(x=0,y=0)
        self.button1 = tkinter.Button(self.win,text = "搜索",command = self.search)  #导入搜索键,command表示绑定search的行为
        self.button1.place(x=200,y=0)
        self.comvalue = tkinter.StringVar()  # 窗体自带文本,新建一个值
        self.comboxlist = ttk.Combobox(self.win, textvariable=self.comvalue,width=100)  # 初始化
        self.comboxlist["values"] = ("listshow", "testshow", "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.comboxlistfile = ttk.Combobox(self.win, textvariable=self.comvaluefile,width=100)  # 初始化
        self.comboxlistfile["values"] = (r"D:\Python代码\class15\图形化编程\txm.txt",
                                         r"D:\Python代码\class15\图形化编程\bigdata.txt",
                                         r"D:\Python代码\class15\图形化编程\tmy.txt")
        self.comboxlistfile.current(0)  # 选择第一个
        self.comboxlistfile.bind("<<ComboboxSelected>>", self.filego)  # 绑定事件与函数
        self.comboxlistfile.place(x=0,y=80)
        self.howtoshowfile=r"D:\Python代码\class15\图形化编程\txm.txt"

    def go(self,*args):
        self.howtoshow=self.comboxlist.get()  #保存选中的值

    def filego(self, *args):
        self.howtoshowfile = self.comboxlistfile.get()  # 保存选中的值

    def call(self,event):
        print(event.keysym)
        if(event.keysym=="return"):
            self.search()

    def search(self):
        print("start search",self.entry.get())  #在运行窗口输出搜索的内容
        big=数据查询.BigDataFind.bigdatafind(self.howtoshowfile,self.howtoshow)
        big.find(self.entry.get())
        big.show()
    def show(self):
        self.win.mainloop()


4.BigDataFind

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.Testshowdata()
        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()
'''
#test
big = bigdatafind("D:\\Python代码\\class15\\图形化编程\\bigdata.txt","show")
big.find("txm")
big.show()
'''

5.ListShow

import 数据查询.BaseWindow
import tkinter
class ListShow(数据查询.BaseWindow.BaseWindowShow):
    def __init__(self):
        数据查询.BaseWindow.BaseWindowShow.__init__(self)
        self.mylist=tkinter.Listbox(self.win,width=200 )  #列表框
        self.mylist.pack()

    def addata(self, inserstr):
        self.mylist.insert(tkinter.END, inserstr)
'''
mylist=ListShow()
mylist.addata("djjiahsjidh")
mylist.show()
'''

6.TextShow

import 数据查询.BaseWindow
import tkinter
class Testshowdata(数据查询.BaseWindow.BaseWindowShow):
    def __init__(self):
        数据查询.BaseWindow.BaseWindowShow.__init__(self)
        self.text=tkinter.Text(self.win)  #文本编辑器
        self.text.pack()
    def addata(self,inserstr):
        self.text.insert(tkinter.INSERT,inserstr)
'''
mylist=Testshowdata()
mylist.addata("djjiahsjidh\r\n")
mylist.addata("djjiahsjidh\r\n")
mylist.show()
'''

7.运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46837674/article/details/113479801