以下是一个示例代码,可以实现上述功能:

import os
import tkinter as tk
from tkinter import ttk
from datetime import datetime


class FileBrowser:
    def __init__(self, root):
        self.root = root
        self.root.title("File Browser")
        self.root.geometry("600x400")

        # 指定路径和扩展名输入框
        self.path_label = ttk.Label(root, text="Path:")
        self.path_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
        self.path_entry = ttk.Entry(root, width=50)
        self.path_entry.grid(row=0, column=1, padx=5, pady=5, sticky="we")
        self.path_entry.insert(0, os.getcwd())
        self.ext_label = ttk.Label(root, text="Extension:")
        self.ext_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
        self.ext_entry = ttk.Entry(root, width=10)
        self.ext_entry.grid(row=1, column=1, padx=5, pady=5, sticky="we")
        self.ext_entry.insert(0, "*")

        # 检索按钮
        self.search_button = ttk.Button(root, text="Search", command=self.search_files)
        self.search_button.grid(row=0, column=2, rowspan=2, padx=5, pady=5, sticky="we")

        # 文件列表窗口
        self.file_list = ttk.Treeview(root, columns=("name", "path", "size", "modified"))
        self.file_list.heading("#0", text="ID")
        self.file_list.heading("name", text="Name")
        self.file_list.heading("path", text="Path")
        self.file_list.heading("size", text="Size")
        self.file_list.heading("modified", text="Modified")
        self.file_list.column("#0", width=50)
        self.file_list.column("name", width=150)
        self.file_list.column("path", width=200)
        self.file_list.column("size", width=100)
        self.file_list.column("modified", width=150)
        self.file_list.grid(row=2, column=0, columnspan=3, padx=5, pady=5, sticky="nesw")
        self.file_list.bind("<Double-1>", self.open_folder)

    def search_files(self):
        path = self.path_entry.get()
        ext = self.ext_entry.get()
        if not os.path.isdir(path):
            tk.messagebox.showerror("Error", "Invalid path!")
            return
        self.file_list.delete(*self.file_list.get_children())
        for root, dirs, files in os.walk(path):
            for file in files:
                if file.endswith(ext):
                    file_path = os.path.join(root, file)
                    file_size = os.path.getsize(file_path)
                    file_modified = datetime.fromtimestamp(os.path.getmtime(file_path)).strftime("%Y-%m-%d %H:%M:%S")
                    self.file_list.insert("", "end", text=len(self.file_list.get_children())+1,
                                          values=(file, root, file_size, file_modified))

    def open_folder(self, event):
        item = self.file_list.selection()[0]
        path = self.file_list.item(item, "values")[1]
        os.startfile(path)


if __name__ == "__main__":
    root = tk.Tk()
    app = FileBrowser(root)
    root.mainloop()

该程序使用了tkinter库来实现GUI界面,使用os库来操作文件系统。程序界面包含一个指定路径和扩展名的输入框,一个检索按钮和一个文件列表窗口。当用户点击检索按钮时,程序会根据指定路径和扩展名检索文件,并将检索到的文件信息展示在列表窗口中。用户可以点击文件名所在的行来打开目标文件所在的文件夹。

我需要使用python编写一个小程序它应该实现以下功能:1、可以检索指定路径中的指定扩展名的文件2、将所有检索到的文件集成到一个列表窗口中这个列表窗口会展示出每个文件的文件名、所在路径、文件大小、修改日期等信息3、在列表窗口中点击文件名所在的行时会自动打开目标文件所在的文件夹

原文地址: https://www.cveoy.top/t/topic/bEgY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录