当前位置: 首页 > news >正文

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

 

相关文章:

  • 关于windows中host文件的修改
  • Python-自制简易程序挂机刷御魂
  • Python-使用geany编辑器实现32位与64位共存使用
  • python-五子棋-AI
  • Python-从视频到gif(imageio,moviepy,ffmpeg)
  • python-二分法插入排序(Binary Insert Sort)
  • 本地仓库关联Github仓库
  • macos可以升级到指定版本吗_iPhone 越狱后还可以保资料升级系统吗?
  • 2 shell 锂基脂_内蒙古锂基脂润滑油供应商
  • python编程中的不等于符号_python不等于符号怎么写
  • nacos 负载策略_Ribbon负载均衡
  • python文件传输模块_Python爱好者 socket模块传输文件 -
  • 红曲面怎么做_曲面屏是华为手机一大败笔?确实缺点很多,但华为为何钟爱呢?...
  • 高德地图 python面试_【高德地图面试|面试题】-看准网
  • tablayout 滚动模式_scrollview+tablayout实现联动
  • [译]如何构建服务器端web组件,为何要构建?
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • Laravel Mix运行时关于es2015报错解决方案
  • mongo索引构建
  • node学习系列之简单文件上传
  • Python爬虫--- 1.3 BS4库的解析器
  • Redis 中的布隆过滤器
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • 包装类对象
  • 测试如何在敏捷团队中工作?
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 力扣(LeetCode)965
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 正则与JS中的正则
  • ​iOS实时查看App运行日志
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • (14)Hive调优——合并小文件
  • (层次遍历)104. 二叉树的最大深度
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .Net中wcf服务生成及调用
  • .net中的Queue和Stack
  • .Net组件程序设计之线程、并发管理(一)
  • @angular/cli项目构建--Dynamic.Form
  • @modelattribute注解用postman测试怎么传参_接口测试之问题挖掘
  • [ACL2022] Text Smoothing: 一种在文本分类任务上的数据增强方法
  • [cb]UIGrid+UIStretch的自适应
  • [datastore@cyberfear.com].Elbie、[thekeyishere@cock.li].Elbie勒索病毒数据怎么处理|数据解密恢复
  • [Docker]五.Docker中Dockerfile详解
  • [HJ56 完全数计算]
  • [HTTP]HTTP协议的状态码
  • [MYSQL]mysql常用操作命令
  • [Pytorch] pytorch笔记 三
  • [spark] DataFrame 的 checkpoint
  • [three.js]UV动画
  • [Web开发] 在线 Javascript 代码格式美化工具