GUI编程练习(Python)-自制简易的文件检索器

降智警告:本人为编程新手,遵守面向CSDN编程原则,代码架构具有个人特色,仅供参考

注意:这个项目虽然是GUI编程练习,但涉及的方面包括但不限于GUI编程

 

(一)最终结果

源码:在我的Github

功能:在大致范围中检索文件,支持单个关键词检索(关键词可以为文件格式或部分文件名)。检索结果为符合要求的所有文件或文件夹(完整路径),可以将检索结果输出为同一目录下的result.txt文件。

显示:使用自定义的程序图标和程序框图标

 

(二)具体实现

使用的库如下:

import os
import base64
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import *
import tkinter as tk
from in_ico import img

1.程序框图标设置与初步架构

if __name__ == "__main__" :
    
    res = ''	#检索结果或错误代码
    change = False #记录实时检索结果是否发生改变
    finish = False	#记录检索是否完成
    
    #使用自定义程序框图标
    
    tmp = open('in.ico', 'wb')
    tmp.write(base64.b64decode(img))
    tmp.close()
    
    #程序框架构
    
    root = Tk()
    root.title('Search')
    root.iconbitmap('in.ico')
    os.remove('in.ico')
    
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    w = 400
    h = 250
    x = (ws/2) - (w/2) 
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))
    root.resizable(width=False, height=False)
    
    using = use(root)
    
    root.mainloop()

geometry()函数的参数为程序框尺寸(长x宽) + 初始位置(横坐标+纵坐标)

 

2.检索函数(search(path, doc, id_code))

def search(path, doc, id_code) :
    things = os.listdir(path)
    for thing in things :
        new_path = os.path.join(path, thing)
        if doc in thing : 
            global res
            global change
            res += new_path
            res += '\n'
            change = True
            continue
        if os.path.isfile(new_path): continue
        if os.path.isdir(new_path) :
            search(new_path, doc, 0)
        
    if id_code == 1 :
        global finish
        finish = True

path为当前函数检索路径,doc为检索关键词,id_code为函数标识码

id_code为1的是最开始的检索函数,其余子函数均为0,当所有子函数执行完成后主函数将finish置为True,表示检索完成

 

3.程序框架构(use类)

①__init__函数

def __init__(self, root) :
    frame = Frame(root)
    frame.place(x = 0, y = 0, width = 600, height = 600)
    self.lab1 = Label(frame, text='文件名:')
    self.lab2 = Label(frame, text='大概位置:')
    self.lab3 = Label(frame, text = '查找结果')
    self.en1 = Entry(frame, width = 40) #文件名
    self.en2 = Entry(frame, width = 40) #大致路径
    #查找结果
    self.text = Text(frame, height = 6, width = 40)
    self.scr = Scrollbar(frame)
    self.control = False
        
    self.but1 = Button(frame, text = '查找', command = self.find)
    self.but2 = Button(frame, text = '重置', command = self.clean)
    self.but3 = Button(frame, text = '输出', command = self.output)
    self.but4 = Button(frame, text = '退出', command = root.quit)
        
    self.place()

包括三个Label控件进行标识,两个Entry控件实现用户输入,一个Text控件实现结果显示,一个Scrollbar控件实现滚动条,control变量用于区分Text控件框是用户输入还是检索结果,四个Button控件实现四个用户功能,place()函数进行控件放置

②clean函数

def clean(self) :
    global res
    res = ''
        
    self.en1.delete(0, END)
    self.en2.delete(0, END)
    self.text.delete(1.0, END)

重置两个输入框和Text显示框中的内容,并将res置空

③output函数

def output(self) :
    cont = self.text.get(1.0, END)
    error_code = ['No Path\n', 'Path Error\n', 'No File\n', 'File Not Found\n']
    if cont in error_code : return
    if cont == '\n' : return 
        
    if self.control == False : return

    path = os.getcwd()
    document = 'result.txt'
    final = os.path.join(path, document)
        
    out = open(final, 'w')
    out.write(cont)
    out.close()

对Text中的内容进行比对,仅当进行检索后且不为错误代码时进行输出

④place函数

def place(self) :
    
    self.lab1.place(x = 20, y = 20)
    self.lab2.place(x = 20, y = 60)
    self.lab3.place(x = 20, y = 100)
        
    self.en1.place(x = 80, y = 20) #文件名
    self.en2.place(x = 80, y = 60)  #大致路径
        
    #查找结果
    self.text.place(x = 80, y = 100)
    self.scr.place(x = 364, y = 100)
        
    self.but1.place(x = 60, y = 200)
    self.but2.place(x = 140, y = 200)
    self.but3.place(x = 220, y = 200)
    self.but4.place(x = 300, y = 200)
        
    self.scr.config(command = self.text.yview)
    self.text.config(yscrollcommand = self.scr.set)

对控件进行放置并将Text控件与滚动条绑定

⑤find函数

def find(self) :
    global res
    res = ''
    self.text.delete(1.0,END)
    che = self.check()
        
    if che :
        doc = self.en1.get()
        path = self.en2.get()
        search(path, doc, 1)
        self.print_text()
            
        if res == '' :
            self.text.insert(1.0, "File Not Found")
            return
            
        elif change == True :
            self.control = True
            self.text.insert(1.0, res)
            change == False
                
        self.text.insert(END, '\nFinish')
            
            
    else :
        self.text.insert(1.0, res)

先对两个输入框中的内容进行检查(check函数),均正确时进行检索。对实时检索结果进行显示(print_text函数)

⑥print_text函数

def print_text(self) :
    global change
    global finish
    while True :
        if change == True :
            self.control = True
            self.text.insert(1.0, res)
            change == False
        if finish == True : break

每当res更新时对Text中内容进行输出并将control置为True

⑦check函数

​
def check(self) :
    doc = self.en1.get()
    path = self.en2.get()
    global res
        
    if path == '' :
        res = "No Path"
        return False
            
    elif os.path.isdir(path) == False :
        res = "Path Error"
        return False
        
    elif doc == '' :
        res = "No File"
        return False
            
    else : return True

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值