| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- """
- This is a new Python file created to display a directory tree as a table
- with width set to 90% of the window using tkinter GUI.
- """
- import os
- import tkinter as tk
- class DirectoryTableGUI:
- def __init__(self, root):
- self.root = root
- self.root.title("Directory Tree Table")
- # Set window size to 1200x1000 pixels
- self.root.geometry("1200x1000")
-
- # Calculate 90% of window width
- table_width = int(1200 * 0.9) # 90% of 1200 = 1080
-
- # Configure the main frame
- main_frame = tk.Frame(root)
- main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
-
- # Create a label to indicate the table
- table_label = tk.Label(main_frame, text="Directory Tree Table (Width: 90% of window)",
- font=("Arial", 14, "bold"))
- table_label.pack(pady=(0, 10))
-
- # Create a frame for the table with specified width
- table_frame = tk.Frame(main_frame, width=table_width, bg="black",
- relief=tk.RAISED, bd=2)
- table_frame.pack_propagate(False) # Prevent frame from shrinking to fit contents
- table_frame.pack(padx=10)
-
- # Define the root directory path
- root_dir = r"E:\agricultural_research_platform"
- root_name = os.path.basename(root_dir)
-
- # Count the number of directories in the root directory
- try:
- items = os.listdir(root_dir)
- directories = [item for item in items if os.path.isdir(os.path.join(root_dir, item))]
- original_n_dirs = len(directories) # Keep track of original count
-
- # Limit to 20 directories + 1 for files, with an extra slot for ellipsis if needed
- if original_n_dirs > 20:
- directories = directories[:20] # Take only first 20
- n_dirs = 20
- has_more_dirs = True
- else:
- n_dirs = original_n_dirs
- has_more_dirs = False
-
- # Calculate total columns: n_dirs + 1 for files + 1 for ellipsis (if needed)
- n_cols_second_row = n_dirs + 1 # n directories + 1 for files
- if has_more_dirs:
- n_cols_second_row += 1 # Add one more for ellipsis
- except FileNotFoundError:
- n_dirs = 0
- n_cols_second_row = 1 # Just one column for "no directories found"
- directories = []
- has_more_dirs = False
-
- print(f"DEBUG: Found {original_n_dirs} directories, showing {n_dirs}, has_more_dirs={has_more_dirs}, n_cols_second_row={n_cols_second_row}")
-
- # Create merged cell for the first row spanning all columns of the second row
- cell_width = table_width # Full width for the merged cell
-
- # First row: merged cell containing root directory name
- root_cell = tk.Label(table_frame,
- text=f"Root: {root_name} ({original_n_dirs} folders)",
- bg="lightyellow",
- relief=tk.RAISED,
- bd=1,
- font=("Arial", 14, "bold"),
- width=cell_width//8, # Approximate character width
- height=3,
- wraplength=cell_width-20) # Wrap text if needed
- root_cell.grid(row=0, column=0, columnspan=n_cols_second_row, sticky="nsew", padx=1, pady=1) # columnspan merges all columns of second row
-
- # Second row: create cells (up to 20 directories + 1 for files + 1 for ellipsis if needed)
- for col in range(n_cols_second_row):
- # Calculate width for each cell (table_width divided by n_cols_second_row columns)
- cell_width = table_width // n_cols_second_row
-
- if col < n_dirs:
- # Directory cell
- cell_text = f"[{directories[col]}]"
- cell_bg = "lightgreen"
- elif col == n_dirs and has_more_dirs:
- # Ellipsis cell to indicate more directories
- cell_text = f"... (+{original_n_dirs - 20})"
- cell_bg = "lightgray"
- elif col == n_cols_second_row - 1: # Last column is always files
- # Files cell (always in the last position)
- files_list = [item for item in items if os.path.isfile(os.path.join(root_dir, item))]
- cell_text = f"All Files ({len(files_list)})"
- cell_bg = "lightcoral"
- else:
- # Fallback for any remaining indices
- cell_text = f"Col {col+1}"
- cell_bg = "lightblue"
-
- print(f"DEBUG: Creating cell at col {col}, text: {cell_text}")
-
- cell = tk.Label(table_frame,
- text=cell_text,
- bg=cell_bg,
- relief=tk.RAISED,
- bd=1,
- font=("Arial", 10),
- width=cell_width//8, # Approximate character width
- height=5,
- wraplength=cell_width//2) # Wrap text if needed
- cell.grid(row=1, column=col, sticky="nsew", padx=1, pady=1)
-
- # Configure grid weights so cells expand proportionally
- for i in range(n_cols_second_row):
- table_frame.grid_columnconfigure(i, weight=1)
- for i in range(2): # Two rows for now
- table_frame.grid_rowconfigure(i, weight=1)
- def main():
- # Create the main window
- root = tk.Tk()
-
- # Initialize the directory table GUI
- app = DirectoryTableGUI(root)
-
- # Start the GUI event loop
- root.mainloop()
- if __name__ == "__main__":
- main()
|