import os import tkinter as tk from tkinter import filedialog, scrolledtext, ttk # 搜索指定关键字的函数 def search_keyword_in_files(directory, keyword, output_text): if not keyword: output_text.insert(tk.END, "Please provide a keyword to search.\n") return # 递归遍历目录中的所有文件 for root, dirs, files in os.walk(directory): for file in files: # 只检查 .js, .json, .vue, .ts 等相关文件 if file.endswith(('.js', '.json', '.vue', '.ts')): file_path = os.path.join(root, file) try: # 打开文件并检查是否包含指定的关键字 with open(file_path, 'r', encoding='utf-8') as f: content = f.read() if keyword in content: output_text.insert(tk.END, f"Found '{keyword}' in: {file_path}\n") except Exception as e: output_text.insert(tk.END, f"Error reading file {file_path}: {e}\n") output_text.insert(tk.END, "Search completed.\n") # 浏览文件夹并执行搜索 def browse_directory_for_search(keyword_entry, output_text): directory = filedialog.askdirectory() if directory: keyword = keyword_entry.get() output_text.insert(tk.END, f"Searching in directory: {directory} for keyword: '{keyword}'\n") search_keyword_in_files(directory, keyword, output_text) # 清空日志窗口 def clear_output(output_text): output_text.delete(1.0, tk.END) # 替换文件内容中的指定字符串 def replace_in_files(directory, search_text, replace_text, output_text): if not search_text or not replace_text: output_text.insert(tk.END, "Please provide both search and replace text.\n") return for root, dirs, files in os.walk(directory): for file in files: if file.endswith(('.js', '.json', '.vue', '.ts')): file_path = os.path.join(root, file) try: # 打开文件并读取内容 with open(file_path, 'r', encoding='utf-8') as f: content = f.read() # 如果找到了 search_text,进行替换 if search_text in content: new_content = content.replace(search_text, replace_text) # 将新的内容写回文件 with open(file_path, 'w', encoding='utf-8') as f: f.write(new_content) output_text.insert(tk.END, f"Replaced in: {file_path}\n") except Exception as e: output_text.insert(tk.END, f"Error processing file {file_path}: {e}\n") output_text.insert(tk.END, "Replacement completed.\n") # 浏览文件夹并执行替换 def browse_directory_for_replace(search_entry, replace_entry, output_text): directory = filedialog.askdirectory() if directory: search_text = search_entry.get() replace_text = replace_entry.get() output_text.insert(tk.END, f"Replacing in directory: {directory}\n") output_text.insert(tk.END, f"Replacing '{search_text}' with '{replace_text}'\n") replace_in_files(directory, search_text, replace_text, output_text) # 创建主窗口 def create_gui(): root = tk.Tk() root.title("File Text Search and Replace Tool") root.geometry("800x500") # 创建选项卡 notebook = ttk.Notebook(root) notebook.pack(fill='both', expand=True) # 第一个选项卡:关键字搜索 search_frame = ttk.Frame(notebook) notebook.add(search_frame, text="Search") # 第二个选项卡:替换内容 replace_frame = ttk.Frame(notebook) notebook.add(replace_frame, text="Replace") # 搜索功能:左侧面板,用于输入关键字和查找文件 left_frame_search = ttk.Frame(search_frame, width=200) left_frame_search.pack(side=tk.LEFT, fill=tk.Y, padx=10, pady=10) keyword_label = ttk.Label(left_frame_search, text="Keyword to Search:") keyword_label.pack(pady=5) keyword_entry = ttk.Entry(left_frame_search, width=30) keyword_entry.pack(pady=5) browse_button_search = ttk.Button(left_frame_search, text="Browse Directory", command=lambda: browse_directory_for_search(keyword_entry, output_text_search)) browse_button_search.pack(pady=10) clear_button_search = ttk.Button(left_frame_search, text="Clear Output", command=lambda: clear_output(output_text_search)) clear_button_search.pack(pady=5) output_text_search = scrolledtext.ScrolledText(search_frame, width=70, height=25) output_text_search.pack(side=tk.RIGHT, padx=10, pady=10, fill=tk.BOTH, expand=True) # 替换功能:左侧面板,提供替换功能 left_frame_replace = ttk.Frame(replace_frame, width=200) left_frame_replace.pack(side=tk.LEFT, fill=tk.Y, padx=10, pady=10) search_label = ttk.Label(left_frame_replace, text="Text to Search:") search_label.pack(pady=5) search_entry = ttk.Entry(left_frame_replace, width=30) search_entry.pack(pady=5) replace_label = ttk.Label(left_frame_replace, text="Text to Replace:") replace_label.pack(pady=5) replace_entry = ttk.Entry(left_frame_replace, width=30) replace_entry.pack(pady=5) browse_button_replace = ttk.Button(left_frame_replace, text="Browse Directory", command=lambda: browse_directory_for_replace(search_entry, replace_entry, output_text_replace)) browse_button_replace.pack(pady=10) clear_button_replace = ttk.Button(left_frame_replace, text="Clear Output", command=lambda: clear_output(output_text_replace)) clear_button_replace.pack(pady=5) output_text_replace = scrolledtext.ScrolledText(replace_frame, width=70, height=25) output_text_replace.pack(side=tk.RIGHT, padx=10, pady=10, fill=tk.BOTH, expand=True) root.mainloop() if __name__ == "__main__": create_gui()