Advertisement
Najeebsk

Run.pyw

Aug 27th, 2024 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.22 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import messagebox, ttk
  4.  
  5. def load_folders():
  6.     folders = []
  7.     try:
  8.         with open('FOLDERS.txt', 'r', encoding='utf-8', errors='replace') as file:
  9.             folders = [line.strip() for line in file if line.strip()]
  10.     except FileNotFoundError:
  11.         messagebox.showerror("Error", "FOLDERS.txt file not found.")
  12.     return folders
  13.  
  14. def load_commands():
  15.     commands = []
  16.     try:
  17.         with open('Commands.txt', 'r', encoding='utf-8', errors='replace') as file:
  18.             commands = [line.strip() for line in file if line.strip()]
  19.     except FileNotFoundError:
  20.         messagebox.showerror("Error", "Commands.txt file not found.")
  21.     return commands
  22.  
  23. def list_items(search_term=None):
  24.     listbox.delete(0, tk.END)
  25.     for folder in folders:
  26.         if os.path.exists(folder):
  27.             for item in os.listdir(folder):
  28.                 if not search_term or search_term.lower() in item.lower():
  29.                     listbox.insert(tk.END, item)
  30.         else:
  31.             messagebox.showerror("Error", f"Folder not found: {folder}")
  32.  
  33. def search_items():
  34.     search_term = search_entry.get()
  35.     list_items(search_term)
  36.  
  37. def open_selected(event=None):
  38.     selected = listbox.get(tk.ACTIVE)
  39.     if selected:
  40.         for folder in folders:
  41.             full_path = os.path.join(folder, selected)
  42.             if os.path.exists(full_path):
  43.                 # Check if the selected file has an allowed extension
  44.                 if selected.endswith(('.txt', '.py', '.pyw', '.ahk', '.bat', '.m3u', '.m3u8', '.reg', '.vbs', '.ls')):
  45.                     try:
  46.                         with open(full_path, 'r', encoding='utf-8', errors='replace') as file:
  47.                             second_result_text.delete(1.0, tk.END)
  48.                             second_result_text.insert(tk.END, file.read())
  49.                             second_result_text.config(state=tk.NORMAL)  # Ensure the second result_text field is editable
  50.                     except Exception as e:
  51.                         messagebox.showerror("Error", str(e))
  52.                 else:
  53.                     os.startfile(full_path)
  54.                 break
  55.  
  56. def run_command():
  57.     command = command_entry.get() or command_dropdown.get()
  58.     if command:
  59.         try:
  60.             os.system(command)
  61.         except Exception as e:
  62.             messagebox.showerror("Error", str(e))
  63.            
  64.  
  65. # GUI setup
  66. root = tk.Tk()
  67. root.title("Najeeb File Folder Explorer and Command Runner")
  68. root.geometry("900x640")
  69. root.configure(bg="#FFD700")  # Set background color of the window
  70.  
  71. # Load folder paths from FOLDERS.txt file
  72. folders = load_folders()
  73.  
  74. # Load commands from Commands.txt file
  75. commands = load_commands()
  76.  
  77. # Frame for the search field and button
  78. search_frame = tk.Frame(root)
  79. search_frame.pack(pady=10)
  80.  
  81. # Search entry (top search result text field)
  82. search_entry = tk.Entry(search_frame, font=("Arial", 12), bg="white", fg="black", width=68)
  83. search_entry.pack(side=tk.LEFT, padx=5)
  84.  
  85. # Search button
  86. search_button = tk.Button(search_frame, font=("Arial", 12), bg="blue", fg="white", text="Search", command=search_items)
  87. search_button.pack(side=tk.LEFT)
  88.  
  89. # Frame to hold the Listbox and Scrollbars
  90. listbox_frame = tk.Frame(root)
  91. listbox_frame.pack(pady=10)
  92.  
  93. # Listbox to show folder contents (editable)
  94. listbox = tk.Listbox(listbox_frame, selectmode=tk.SINGLE, font=("Arial", 12), bg="blue", fg="white", width=96, height=15)
  95. listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  96.  
  97. # Vertical scrollbar
  98. v_scrollbar = tk.Scrollbar(listbox_frame, orient=tk.VERTICAL, command=listbox.yview)
  99. v_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  100.  
  101. # Configuring Listbox scrollbars
  102. listbox.config(yscrollcommand=v_scrollbar.set)
  103.  
  104. # Frame for the second result text field
  105. result_frame2 = tk.Frame(root)
  106. result_frame2.pack(pady=5)
  107.  
  108. # Second text field (for opening .txt files on double-click)
  109. second_result_text = tk.Text(result_frame2, font=("Arial", 12), bg="white", fg="black", height=12, width=96)
  110. second_result_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  111.  
  112. # Vertical scrollbar for the second result text field
  113. result_scrollbar2 = tk.Scrollbar(result_frame2, orient=tk.VERTICAL, command=second_result_text.yview)
  114. result_scrollbar2.pack(side=tk.RIGHT, fill=tk.Y)
  115.  
  116. # Configuring the text field scrollbar
  117. second_result_text.config(yscrollcommand=result_scrollbar2.set)
  118.  
  119. # Bind double-click event to open the selected file in the second text field
  120. listbox.bind("<Double-1>", open_selected)
  121.  
  122. # Frame for Run field and button
  123. run_frame = tk.Frame(root)
  124. run_frame.pack(pady=5)
  125.  
  126. # Dropdown list for commands from Commands.txt
  127. command_dropdown = ttk.Combobox(run_frame, font=("Times New Roman", 16), values=commands, width=45)
  128. command_dropdown.pack(side=tk.LEFT, padx=5)
  129.  
  130. # Command entry (Run field)
  131. command_entry = tk.Entry(run_frame, font=("Times New Roman", 16), bg="green", fg="white", width=20)
  132. command_entry.pack(side=tk.LEFT, padx=5)
  133.  
  134. # Run button on the same line as the Run field and dropdown
  135. run_button = tk.Button(run_frame, font=("Arial", 12), bg="blue", fg="white", text="Run commands", command=run_command)
  136. run_button.pack(side=tk.LEFT, padx=5)
  137.  
  138. # Load folder contents on startup
  139. list_items()
  140.  
  141. root.mainloop()
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement