Advertisement
Mark2020H

Treeview with python 3 Makes finding folders files easy Ideal for coders , programmers

Jul 20th, 2023 (edited)
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # You will need to install the Lato-Medium.ttf font on this
  4. # To do this use  https://fonts.google.com/specimen/Lato?query=Lato  and download font family
  5.  
  6. # This will downlaod a zip
  7. # Open this zip folder and extract then goto that folder and double click the Lato-Medium.ttf file
  8. # This will open system font viewer  and within this is and install button  
  9. # Install this font  for system wide all users
  10. # MD Harrington 20/07/23  London UK Kent
  11.  
  12. import os
  13. import colorama
  14. from colorama import Fore, Style
  15. import time
  16. from PIL import Image, ImageDraw, ImageFont
  17. import tkinter as tk
  18. from PIL import ImageTk
  19.  
  20. class Tree_Creator:
  21.  
  22.     @staticmethod
  23.     def list_files(folder_path, indent="", file=None):
  24.         output_text = ""
  25.         for item in os.listdir(folder_path):
  26.             item_path = os.path.join(folder_path, item)
  27.             if os.path.isdir(item_path):
  28.                 output_text += (indent + "|-- " + item + "/\n")
  29.                 Tree_Creator.typewriter_print(indent + "|-- " + item + "/\n", color=Fore.YELLOW)
  30.                 file.write(indent + "|-- " + item + "/\n")
  31.                 output_text += Tree_Creator.list_files(item_path, indent + "    ", file)
  32.             else:
  33.                 output_text += (indent + "|-- " + item + "\n")
  34.                 Tree_Creator.typewriter_print(indent + "|-- " + item + "\n", color=Fore.RED)
  35.                 file.write(indent + "|-- " + item + "\n")
  36.         return output_text
  37.  
  38.     @staticmethod
  39.     def main():
  40.         # Prompt user for folder name
  41.         Tree_Creator.typewriter_print("Enter a folder name: ", color=Fore.CYAN)
  42.         folder_name = input(": ")
  43.  
  44.         # Search for the folder in the user's home area
  45.         home_directory = os.path.expanduser("~")
  46.         folder_path = None
  47.         for root, dirs, files in os.walk(home_directory):
  48.             if folder_name in dirs:
  49.                 folder_path = os.path.join(root, folder_name)
  50.                 break
  51.  
  52.         # Verify if the folder exists
  53.         if folder_path is None:
  54.             print("Folder not found.")
  55.             exit(1)
  56.  
  57.         # Change to the directory
  58.         os.chdir(folder_path)
  59.  
  60.         # Save folder structure to file
  61.         output_filename = "Project_Tree.txt"
  62.         with open(output_filename, "w") as file:
  63.             file.write("Folder Structure:\n")
  64.             output_text = Tree_Creator.list_files(folder_path, file=file)
  65.  
  66.         # Create and display the image
  67.         Tree_Creator.create_image(output_text, "folder_structure.png")
  68.  
  69.         # Print folder structure saved message
  70.         Tree_Creator.typewriter_print("Folder structure saved to: ", color=Fore.GREEN)
  71.         Tree_Creator.typewriter_print(os.path.abspath(output_filename), color=Fore.CYAN)
  72.  
  73.     @staticmethod
  74.     def typewriter_print(text, color=Fore.WHITE, delay=0.04):
  75.         for char in text:
  76.             print(color + char, end='', flush=True)
  77.             time.sleep(delay)
  78.         print(Style.RESET_ALL)
  79.  
  80.     @staticmethod
  81.    
  82.     def create_image(text, output_filename):
  83.         # Set the font properties
  84.         font_size = 20  # Updated font size
  85.         line_spacing = 10  # Extra line spacing
  86.         font_path = "/usr/share/fonts/truetype/lato/Lato-Medium.ttf"
  87.         font = ImageFont.truetype(font_path, font_size)
  88.  
  89.         # Create a sample image to calculate line height and maximum line width
  90.         sample_image = Image.new("RGB", (1, 1))
  91.         sample_draw = ImageDraw.Draw(sample_image)
  92.         line_height = sample_draw.textbbox((0, 0), "A", font=font)[3]  # Calculate line height based on a sample character
  93.         line_height_with_spacing = line_height + line_spacing  # Line height with extra spacing
  94.         max_line_width = max(sample_draw.textbbox((0, 0), line, font=font)[2] for line in text.split("\n"))  # Calculate maximum line width
  95.  
  96.         # Calculate the image size based on the text
  97.         image_width = int(max_line_width) + 20
  98.         image_height = int(line_height_with_spacing) * len(text.split("\n")) + 20
  99.  
  100.         # Create a new image
  101.         image = Image.new("RGB", (image_width, image_height), (0, 0, 255))
  102.         draw = ImageDraw.Draw(image)
  103.  
  104.         # Draw the text on the image with extra line spacing
  105.         y = 10
  106.         for line in text.split("\n"):
  107.             draw.text((10, y), line, fill=(0, 0, 0), font=font)
  108.             y += line_height_with_spacing
  109.  
  110.         # Display the image in a GUI window
  111.         root = tk.Tk()
  112.         root.title("Image Viewer")
  113.  
  114.         img = ImageTk.PhotoImage(Image.open(output_filename))
  115.         panel = tk.Label(root, image=img)
  116.         panel.pack()
  117.  
  118.         root.mainloop()
  119.  
  120.  
  121.  
  122.  
  123. # Run the main method if executed as a script
  124. if __name__ == '__main__':
  125.     colorama.init()
  126.     Tree_Creator.main() # RcvSerHDF5
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement