new.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """
  2. This is a new Python file created to display a directory tree as a table
  3. with width set to 90% of the window using tkinter GUI.
  4. """
  5. import os
  6. import tkinter as tk
  7. class DirectoryTableGUI:
  8. def __init__(self, root):
  9. self.root = root
  10. self.root.title("Directory Tree Table")
  11. # Set window size to 1200x1000 pixels
  12. self.root.geometry("1200x1000")
  13. # Calculate 90% of window width
  14. table_width = int(1200 * 0.9) # 90% of 1200 = 1080
  15. # Configure the main frame
  16. main_frame = tk.Frame(root)
  17. main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
  18. # Create a label to indicate the table
  19. table_label = tk.Label(main_frame, text="Directory Tree Table (Width: 90% of window)",
  20. font=("Arial", 14, "bold"))
  21. table_label.pack(pady=(0, 10))
  22. # Create a frame for the table with specified width
  23. table_frame = tk.Frame(main_frame, width=table_width, bg="black",
  24. relief=tk.RAISED, bd=2)
  25. table_frame.pack_propagate(False) # Prevent frame from shrinking to fit contents
  26. table_frame.pack(padx=10)
  27. # Define the root directory path
  28. root_dir = r"E:\agricultural_research_platform"
  29. root_name = os.path.basename(root_dir)
  30. # Count the number of directories in the root directory
  31. try:
  32. items = os.listdir(root_dir)
  33. directories = [item for item in items if os.path.isdir(os.path.join(root_dir, item))]
  34. original_n_dirs = len(directories) # Keep track of original count
  35. # Limit to 20 directories + 1 for files, with an extra slot for ellipsis if needed
  36. if original_n_dirs > 20:
  37. directories = directories[:20] # Take only first 20
  38. n_dirs = 20
  39. has_more_dirs = True
  40. else:
  41. n_dirs = original_n_dirs
  42. has_more_dirs = False
  43. # Calculate total columns: n_dirs + 1 for files + 1 for ellipsis (if needed)
  44. n_cols_second_row = n_dirs + 1 # n directories + 1 for files
  45. if has_more_dirs:
  46. n_cols_second_row += 1 # Add one more for ellipsis
  47. except FileNotFoundError:
  48. n_dirs = 0
  49. n_cols_second_row = 1 # Just one column for "no directories found"
  50. directories = []
  51. has_more_dirs = False
  52. 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}")
  53. # Create merged cell for the first row spanning all columns of the second row
  54. cell_width = table_width # Full width for the merged cell
  55. # First row: merged cell containing root directory name
  56. root_cell = tk.Label(table_frame,
  57. text=f"Root: {root_name} ({original_n_dirs} folders)",
  58. bg="lightyellow",
  59. relief=tk.RAISED,
  60. bd=1,
  61. font=("Arial", 14, "bold"),
  62. width=cell_width//8, # Approximate character width
  63. height=3,
  64. wraplength=cell_width-20) # Wrap text if needed
  65. 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
  66. # Second row: create cells (up to 20 directories + 1 for files + 1 for ellipsis if needed)
  67. for col in range(n_cols_second_row):
  68. # Calculate width for each cell (table_width divided by n_cols_second_row columns)
  69. cell_width = table_width // n_cols_second_row
  70. if col < n_dirs:
  71. # Directory cell
  72. cell_text = f"[{directories[col]}]"
  73. cell_bg = "lightgreen"
  74. elif col == n_dirs and has_more_dirs:
  75. # Ellipsis cell to indicate more directories
  76. cell_text = f"... (+{original_n_dirs - 20})"
  77. cell_bg = "lightgray"
  78. elif col == n_cols_second_row - 1: # Last column is always files
  79. # Files cell (always in the last position)
  80. files_list = [item for item in items if os.path.isfile(os.path.join(root_dir, item))]
  81. cell_text = f"All Files ({len(files_list)})"
  82. cell_bg = "lightcoral"
  83. else:
  84. # Fallback for any remaining indices
  85. cell_text = f"Col {col+1}"
  86. cell_bg = "lightblue"
  87. print(f"DEBUG: Creating cell at col {col}, text: {cell_text}")
  88. cell = tk.Label(table_frame,
  89. text=cell_text,
  90. bg=cell_bg,
  91. relief=tk.RAISED,
  92. bd=1,
  93. font=("Arial", 10),
  94. width=cell_width//8, # Approximate character width
  95. height=5,
  96. wraplength=cell_width//2) # Wrap text if needed
  97. cell.grid(row=1, column=col, sticky="nsew", padx=1, pady=1)
  98. # Configure grid weights so cells expand proportionally
  99. for i in range(n_cols_second_row):
  100. table_frame.grid_columnconfigure(i, weight=1)
  101. for i in range(2): # Two rows for now
  102. table_frame.grid_rowconfigure(i, weight=1)
  103. def main():
  104. # Create the main window
  105. root = tk.Tk()
  106. # Initialize the directory table GUI
  107. app = DirectoryTableGUI(root)
  108. # Start the GUI event loop
  109. root.mainloop()
  110. if __name__ == "__main__":
  111. main()