SHARE
    TWEET
    Najeebsk

    CHEAT.pyw

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

    AltStyle によって変換されたページ (->オリジナル) /