Advertisement
Najeebsk

CHEATS.pyw

Aug 21st, 2024 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.30 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import scrolledtext, messagebox
  4. import subprocess
  5.  
  6. # Fixed path to CHEATS folder
  7. CHEATS_FOLDER = os.path.join(os.path.dirname(__file__), 'CHEATS')
  8.  
  9. # Global variable to store the path of the currently opened file
  10. current_file_path = None
  11.  
  12. def search_cheat_sheet():
  13.     global current_file_path
  14.     search_term = search_var.get().strip().lower()
  15.     result = ''
  16.     current_file_path = None  # Reset the current file path on a new search
  17.  
  18.     if not search_term:
  19.         messagebox.showwarning("Input Error", "Please enter a search term.")
  20.         return
  21.    
  22.     if not os.path.exists(CHEATS_FOLDER):
  23.         messagebox.showwarning("Folder Error", "CHEATS folder not found.")
  24.         return
  25.    
  26.     for root, _, files in os.walk(CHEATS_FOLDER):
  27.         for file in files:
  28.             if file.endswith('.txt'):  # Assuming the cheat sheets are stored as .txt files
  29.                 file_path = os.path.join(root, file)
  30.                 with open(file_path, 'r') as f:
  31.                     content = f.readlines()
  32.                     display_flag = False
  33.                     for line in content:
  34.                         if line.strip().lower() == f"--- {search_term} ---":
  35.                             display_flag = True
  36.                             result += line  # Add the heading
  37.                         elif line.startswith("---") and display_flag:
  38.                             break  # Stop if another heading is found
  39.                         elif display_flag:
  40.                             result += line  # Add all lines under the heading
  41.                     if display_flag:
  42.                         current_file_path = file_path  # Save the path of the first matching file
  43.                         break  # Exit the loop once a match is found
  44.         if current_file_path:
  45.             break  # Exit the loop once a match is found
  46.    
  47.     result_text.delete(1.0, tk.END)
  48.    
  49.     if result:
  50.         result_text.insert(tk.END, result)
  51.         highlight_text(result_text, search_term)  # Highlight and scroll to the first result
  52.     else:
  53.         result_text.insert(tk.END, f"No cheat sheets found for heading '{search_term}'.")
  54.  
  55. def search_within_results():
  56.     search_term = search_result_var.get().strip().lower()
  57.     if not search_term:
  58.         messagebox.showwarning("Input Error", "Please enter a search term for the result text.")
  59.         return
  60.    
  61.     highlight_text(result_text, search_term)
  62.  
  63. def highlight_text(text_widget, search_term):
  64.     text_widget.tag_remove("highlight", "1.0", tk.END)
  65.     if search_term:
  66.         start_pos = "1.0"
  67.         first_occurrence = None  # To store the position of the first occurrence
  68.         while True:
  69.             start_pos = text_widget.search(search_term, start_pos, tk.END, nocase=True)
  70.             if not start_pos:
  71.                 break
  72.             end_pos = f"{start_pos}+{len(search_term)}c"
  73.             text_widget.tag_add("highlight", start_pos, end_pos)
  74.             if not first_occurrence:
  75.                 first_occurrence = start_pos  # Store the first occurrence
  76.             start_pos = end_pos
  77.         text_widget.tag_config("highlight", background="yellow", foreground="black")
  78.        
  79.         if first_occurrence:
  80.             text_widget.see(first_occurrence)  # Scroll to the first occurrence
  81.  
  82. def save_edited_text():
  83.     global current_file_path
  84.     edited_content = result_text.get("1.0", tk.END).strip()
  85.    
  86.     if not edited_content:
  87.         messagebox.showwarning("Save Error", "There is no content to save.")
  88.         return
  89.    
  90.     if not current_file_path:
  91.         messagebox.showwarning("Save Error", "No file loaded to save.")
  92.         return
  93.    
  94.     with open(current_file_path, 'w') as file:
  95.         file.write(edited_content)
  96.    
  97.     messagebox.showinfo("Save Successful", f"Content saved successfully to {current_file_path}!")
  98.  
  99. def open_in_cmd():
  100.     selected_text = result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  101.     if selected_text:
  102.         try:
  103.             # Use cmd.exe with /K to run the command and keep the window open
  104.             subprocess.Popen(f'start cmd.exe /K "{selected_text}"', shell=True)
  105.         except Exception as e:
  106.             messagebox.showerror("Execution Error", f"Command failed: {e}")
  107.     else:
  108.         messagebox.showwarning("Selection Error", "No text selected.")
  109.  
  110. def open_in_vlc():
  111.     selected_text = result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  112.     if selected_text:
  113.         vlc_path = r'C:\Program Files\VideoLAN\VLC\vlc.exe'  # Update with the correct path to VLC
  114.         subprocess.Popen([vlc_path, selected_text])
  115.     else:
  116.         messagebox.showwarning("Selection Error", "No text selected.")
  117.  
  118. # Create the main application window
  119. root = tk.Tk()
  120. root.title("Najeeb Cheat Sheet Search")
  121. root.geometry("900x700")
  122. root.configure(bg="#FFD700")  # Set background color of the window
  123.  
  124. # Create and place the search label and entry on the same line
  125. search_var = tk.StringVar()
  126. search_frame = tk.Frame(root, bg="#FFD700")
  127. search_frame.pack(pady=10)
  128.  
  129. search_label = tk.Label(search_frame, text="Search Heading:", font=("Arial", 14), bg="#FFD700", fg="black")
  130. search_label.pack(side=tk.LEFT, padx=5)
  131.  
  132. search_entry = tk.Entry(search_frame, textvariable=search_var, font=("Arial", 14), width=50, bg="#FFFACD", fg="black")
  133. search_entry.pack(side=tk.LEFT, padx=5)
  134.  
  135. # Create and place the colorful search button on the same line
  136. search_button = tk.Button(search_frame, text="Search", font=("Arial", 12), bg="blue", fg="white", command=search_cheat_sheet)
  137. search_button.pack(side=tk.LEFT, padx=5)
  138.  
  139. # Add additional search field and button to search within displayed results
  140. search_result_frame = tk.Frame(root, bg="#FFD700")
  141. search_result_frame.pack(pady=10)
  142.  
  143. search_result_var = tk.StringVar()
  144. search_result_label = tk.Label(search_result_frame, text="Search within Results:", font=("Arial", 14), bg="#FFD700", fg="black")
  145. search_result_label.pack(side=tk.LEFT, padx=5)
  146.  
  147. search_result_entry = tk.Entry(search_result_frame, textvariable=search_result_var, font=("Arial", 14), width=50, bg="#FFFACD", fg="black")
  148. search_result_entry.pack(side=tk.LEFT, padx=5)
  149.  
  150. search_result_button = tk.Button(search_result_frame, text="Search in Result", font=("Arial", 12), bg="green", fg="white", command=search_within_results)
  151. search_result_button.pack(side=tk.LEFT, padx=5)
  152.  
  153. # Create and place the scrolled text widget for displaying results
  154. result_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, font=("Courier", 12), width=98, height=30, bg="lightyellow", fg="black")
  155. result_text.pack(pady=3)
  156.  
  157. # Create a button frame to hold all buttons in one line
  158. button_frame = tk.Frame(root, bg="#FFD700")
  159. button_frame.pack(pady=10)
  160.  
  161. # Add the Edit & Save button on the same line
  162. save_button = tk.Button(button_frame, text="Edit & Save", font=("Arial", 12), bg="orange", fg="white", command=save_edited_text)
  163. save_button.pack(side=tk.LEFT, padx=5)
  164.  
  165. # Add Open CMD and Open VLC buttons on the same line
  166. cmd_button = tk.Button(button_frame, text="Open CMD", font=("Arial", 12), bg="gray", fg="white", command=open_in_cmd)
  167. cmd_button.pack(side=tk.LEFT, padx=5)
  168.  
  169. vlc_button = tk.Button(button_frame, text="Open VLC", font=("Arial", 12), bg="red", fg="white", command=open_in_vlc)
  170. vlc_button.pack(side=tk.LEFT, padx=5)
  171.  
  172. # Start the Tkinter event loop
  173. root.mainloop()
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement