Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import scrolledtext, messagebox
- import subprocess
- # Fixed path to CHEATS folder
- CHEATS_FOLDER = os.path.join(os.path.dirname(__file__), 'CHEAT')
- # Global variable to store the path of the currently opened file
- current_file_path = None
- def search_cheat_sheet():
- global current_file_path
- search_term = search_var.get().strip().lower()
- result = ''
- current_file_path = None # Reset the current file path on a new search
- if not search_term:
- messagebox.showwarning("Input Error", "Please enter a search term.")
- return
- if not os.path.exists(CHEATS_FOLDER):
- messagebox.showwarning("Folder Error", "CHEATS folder not found.")
- return
- for root, _, files in os.walk(CHEATS_FOLDER):
- for file in files:
- if file.endswith(''): # Assuming the cheat sheets are stored as .txt files
- file_path = os.path.join(root, file)
- with open(file_path, 'r') as f:
- content = f.readlines()
- display_flag = False
- for line in content:
- if line.strip().lower() == f"--- {search_term} ---":
- display_flag = True
- result += line # Add the heading
- elif line.startswith("---") and display_flag:
- break # Stop if another heading is found
- elif display_flag:
- result += line # Add all lines under the heading
- if display_flag:
- current_file_path = file_path # Save the path of the first matching file
- break # Exit the loop once a match is found
- if current_file_path:
- break # Exit the loop once a match is found
- result_text.delete(1.0, tk.END)
- if result:
- result_text.insert(tk.END, result)
- highlight_text(result_text, search_term) # Highlight and scroll to the first result
- else:
- result_text.insert(tk.END, f"No cheat sheets found for heading '{search_term}'.")
- def search_within_results():
- search_term = search_result_var.get().strip().lower()
- if not search_term:
- messagebox.showwarning("Input Error", "Please enter a search term for the result text.")
- return
- highlight_text(result_text, search_term)
- def highlight_text(text_widget, search_term):
- text_widget.tag_remove("highlight", "1.0", tk.END)
- if search_term:
- start_pos = "1.0"
- first_occurrence = None # To store the position of the first occurrence
- while True:
- start_pos = text_widget.search(search_term, start_pos, tk.END, nocase=True)
- if not start_pos:
- break
- end_pos = f"{start_pos}+{len(search_term)}c"
- text_widget.tag_add("highlight", start_pos, end_pos)
- if not first_occurrence:
- first_occurrence = start_pos # Store the first occurrence
- start_pos = end_pos
- text_widget.tag_config("highlight", background="yellow", foreground="black")
- if first_occurrence:
- text_widget.see(first_occurrence) # Scroll to the first occurrence
- def save_edited_text():
- global current_file_path
- edited_content = result_text.get("1.0", tk.END).strip()
- if not edited_content:
- messagebox.showwarning("Save Error", "There is no content to save.")
- return
- if not current_file_path:
- messagebox.showwarning("Save Error", "No file loaded to save.")
- return
- with open(current_file_path, 'w') as file:
- file.write(edited_content)
- messagebox.showinfo("Save Successful", f"Content saved successfully to {current_file_path}!")
- def open_in_cmd():
- selected_text = result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
- if selected_text:
- try:
- # Use cmd.exe with /K to run the command and keep the window open
- subprocess.Popen(f'start cmd.exe /K "{selected_text}"', shell=True)
- except Exception as e:
- messagebox.showerror("Execution Error", f"Command failed: {e}")
- else:
- messagebox.showwarning("Selection Error", "No text selected.")
- def open_in_vlc():
- selected_text = result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
- if selected_text:
- vlc_path = r'C:\Program Files\VideoLAN\VLC\vlc.exe' # Update with the correct path to VLC
- subprocess.Popen([vlc_path, selected_text])
- else:
- messagebox.showwarning("Selection Error", "No text selected.")
- # Create the main application window
- root = tk.Tk()
- root.title("Najeeb Cheat Sheet Search")
- root.geometry("900x700")
- root.configure(bg="#FFD700") # Set background color of the window
- # Create and place the search label and entry on the same line
- search_var = tk.StringVar()
- search_frame = tk.Frame(root, bg="#FFD700")
- search_frame.pack(pady=10)
- search_label = tk.Label(search_frame, text="Search Heading:", font=("Arial", 14), bg="#FFD700", fg="black")
- search_label.pack(side=tk.LEFT, padx=5)
- search_entry = tk.Entry(search_frame, textvariable=search_var, font=("Arial", 14), width=50, bg="#FFFACD", fg="black")
- search_entry.pack(side=tk.LEFT, padx=5)
- # Create and place the colorful search button on the same line
- search_button = tk.Button(search_frame, text="Search", font=("Arial", 12), bg="blue", fg="white", command=search_cheat_sheet)
- search_button.pack(side=tk.LEFT, padx=5)
- # Add additional search field and button to search within displayed results
- search_result_frame = tk.Frame(root, bg="#FFD700")
- search_result_frame.pack(pady=10)
- search_result_var = tk.StringVar()
- search_result_label = tk.Label(search_result_frame, text="Search within Results:", font=("Arial", 14), bg="#FFD700", fg="black")
- search_result_label.pack(side=tk.LEFT, padx=5)
- search_result_entry = tk.Entry(search_result_frame, textvariable=search_result_var, font=("Arial", 14), width=50, bg="#FFFACD", fg="black")
- search_result_entry.pack(side=tk.LEFT, padx=5)
- search_result_button = tk.Button(search_result_frame, text="Search in Result", font=("Arial", 12), bg="green", fg="white", command=search_within_results)
- search_result_button.pack(side=tk.LEFT, padx=5)
- # Create and place the scrolled text widget for displaying results
- result_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, font=("Courier", 12), width=98, height=30, bg="lightyellow", fg="black")
- result_text.pack(pady=3)
- # Create a button frame to hold all buttons in one line
- button_frame = tk.Frame(root, bg="#FFD700")
- button_frame.pack(pady=10)
- # Add the Edit & Save button on the same line
- save_button = tk.Button(button_frame, text="Edit & Save", font=("Arial", 12), bg="orange", fg="white", command=save_edited_text)
- save_button.pack(side=tk.LEFT, padx=5)
- # Add Open CMD and Open VLC buttons on the same line
- cmd_button = tk.Button(button_frame, text="Open CMD", font=("Arial", 12), bg="gray", fg="white", command=open_in_cmd)
- cmd_button.pack(side=tk.LEFT, padx=5)
- vlc_button = tk.Button(button_frame, text="Open VLC", font=("Arial", 12), bg="red", fg="white", command=open_in_vlc)
- vlc_button.pack(side=tk.LEFT, padx=5)
- # Start the Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement