EmaSMach

Using a Treeview (python, tkinter)

Feb 24th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. import os
  2. import threading
  3. from tkinter import *
  4. from tkinter import ttk
  5. from tkinter.filedialog import askdirectory
  6.  
  7.  
  8. def search_files(patron, folder):
  9.     todo = os.walk(folder)
  10.     for base_dir, dirs, files in todo:
  11.         for file in files:
  12.             if patron in file:
  13.                 yield((file, base_dir))
  14.  
  15.  
  16. class MainWindow(Frame):
  17.     def __init__(self, *args, **kwargs):
  18.         super().__init__(*args, **kwargs)
  19.  
  20.         self.master.title('File searcher...')
  21.         self.columnconfigure((1), weight=1)
  22.         self.rowconfigure(2, weight=1)
  23.         self.make_widgets()
  24.  
  25.     def make_widgets(self):
  26.         self.search_folder_label = ttk.Label(self, text='Carpeta (folder):')
  27.         self.search_folder_entry = ttk.Entry(self)
  28.         self.search_folder_button = ttk.Button(self, text='Elejir Carpeta (choose folder)', command=self.ask_open_folder_name)
  29.  
  30.         self.search_label = ttk.Label(self, text='Bรบsqueda (pattern):')
  31.         self.search_entry = ttk.Entry(self)
  32.         self.search_button = ttk.Button(self, text='Buscar (search)', command=self.search)
  33.         # table (Treeview) and scrollbars
  34.         self.vbar = ttk.Scrollbar(self)
  35.         self.vbar.grid(row=2, column=3, sticky='ns')
  36.         self.columns = ('archivo', 'carpeta')
  37.         self.search_results_table = ttk.Treeview(self)
  38.         self.search_results_table.config(show='headings', columns=self.columns)
  39.         self.search_results_table.heading('archivo', text='Archivo (File)')
  40.         self.search_results_table.heading('carpeta', text='Carpeta (Folder)')
  41.         self.vbar.config(command=self.search_results_table.yview)
  42.         self.search_results_table.config(yscrollcommand=self.vbar.set)
  43.  
  44.         self.search_folder_label.grid(row=0, column=0)
  45.         self.search_folder_entry.grid(row=0, column=1, sticky='ew')
  46.         self.search_folder_button.grid(row=0, column=2, sticky='ew')
  47.         self.search_label.grid(row=1, column=0)
  48.         self.search_entry.grid(row=1, column=1, sticky='ew')
  49.         self.search_button.grid(row=1, column=2, sticky='ew')
  50.         self.search_results_table.grid(row=2, column=0, columnspan=3, sticky='nsew')
  51.  
  52.     def ask_open_folder_name(self):
  53.         folder = askdirectory(title='Elija una carpeta')
  54.         self.search_folder_entry.delete(0, END)
  55.         self.search_folder_entry.insert(0, folder)
  56.  
  57.     def search(self):
  58.         self.th1 = threading.Thread(target=self.search_func)
  59.         self.th1.start()
  60.  
  61.     def search_func(self):
  62.         folder = self.search_folder_entry.get()
  63.         patron = self.search_entry.get()
  64.         children = self.search_results_table.get_children()
  65.         if children:
  66.             self.search_results_table.delete(*children)
  67.         if folder and patron:
  68.             for resultado in search_files(patron, folder):
  69.                 self.search_results_table.insert('', 0, values=resultado)
  70.  
  71.  
  72. def main():
  73.     root = Tk()
  74.     w = MainWindow(root)
  75.     w.pack(expand=YES, fill=BOTH)
  76.     root.mainloop()
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment