Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import threading
- from tkinter import *
- from tkinter import ttk
- from tkinter.filedialog import askdirectory
- def search_files(patron, folder):
- todo = os.walk(folder)
- for base_dir, dirs, files in todo:
- for file in files:
- if patron in file:
- yield((file, base_dir))
- class MainWindow(Frame):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.master.title('File searcher...')
- self.columnconfigure((1), weight=1)
- self.rowconfigure(2, weight=1)
- self.make_widgets()
- def make_widgets(self):
- self.search_folder_label = ttk.Label(self, text='Carpeta (folder):')
- self.search_folder_entry = ttk.Entry(self)
- self.search_folder_button = ttk.Button(self, text='Elejir Carpeta (choose folder)', command=self.ask_open_folder_name)
- self.search_label = ttk.Label(self, text='Bรบsqueda (pattern):')
- self.search_entry = ttk.Entry(self)
- self.search_button = ttk.Button(self, text='Buscar (search)', command=self.search)
- # table (Treeview) and scrollbars
- self.vbar = ttk.Scrollbar(self)
- self.vbar.grid(row=2, column=3, sticky='ns')
- self.columns = ('archivo', 'carpeta')
- self.search_results_table = ttk.Treeview(self)
- self.search_results_table.config(show='headings', columns=self.columns)
- self.search_results_table.heading('archivo', text='Archivo (File)')
- self.search_results_table.heading('carpeta', text='Carpeta (Folder)')
- self.vbar.config(command=self.search_results_table.yview)
- self.search_results_table.config(yscrollcommand=self.vbar.set)
- self.search_folder_label.grid(row=0, column=0)
- self.search_folder_entry.grid(row=0, column=1, sticky='ew')
- self.search_folder_button.grid(row=0, column=2, sticky='ew')
- self.search_label.grid(row=1, column=0)
- self.search_entry.grid(row=1, column=1, sticky='ew')
- self.search_button.grid(row=1, column=2, sticky='ew')
- self.search_results_table.grid(row=2, column=0, columnspan=3, sticky='nsew')
- def ask_open_folder_name(self):
- folder = askdirectory(title='Elija una carpeta')
- self.search_folder_entry.delete(0, END)
- self.search_folder_entry.insert(0, folder)
- def search(self):
- self.th1 = threading.Thread(target=self.search_func)
- self.th1.start()
- def search_func(self):
- folder = self.search_folder_entry.get()
- patron = self.search_entry.get()
- children = self.search_results_table.get_children()
- if children:
- self.search_results_table.delete(*children)
- if folder and patron:
- for resultado in search_files(patron, folder):
- self.search_results_table.insert('', 0, values=resultado)
- def main():
- root = Tk()
- w = MainWindow(root)
- w.pack(expand=YES, fill=BOTH)
- root.mainloop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment