256 lines
9.0 KiB
Python
256 lines
9.0 KiB
Python
import os
|
|
import tkinter as tk
|
|
from tkinter import filedialog, scrolledtext, ttk
|
|
from tkinter import Menu
|
|
from PIL import Image
|
|
|
|
|
|
# 搜索指定关键字的函数
|
|
def search_keyword_in_files(directory, keyword, output_text):
|
|
if not keyword:
|
|
output_text.insert(tk.END, "请输入要搜索的关键字。\n")
|
|
return
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
for file in files:
|
|
if file.endswith(
|
|
(".js", ".json", ".vue", ".ts", ".py", ".java", ".cpp", ".xml")
|
|
):
|
|
file_path = os.path.join(root, file)
|
|
try:
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
count = content.count(keyword)
|
|
if count > 0:
|
|
output_text.insert(
|
|
tk.END,
|
|
f"在 {file_path} 中找到 '{keyword}',共 {count} 次\n",
|
|
)
|
|
except Exception as e:
|
|
output_text.insert(tk.END, f"读取文件 {file_path} 出错: {e}\n")
|
|
output_text.insert(tk.END, "搜索完成。\n")
|
|
|
|
|
|
# 找出不包含指定关键字的文件
|
|
def find_files_without_keyword(directory, keyword, output_text):
|
|
if not keyword:
|
|
output_text.insert(tk.END, "请输入要搜索的关键字。\n")
|
|
return
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
for file in files:
|
|
if file.endswith(
|
|
(".js", ".json", ".vue", ".ts", ".py", ".java", ".cpp", ".xml")
|
|
):
|
|
file_path = os.path.join(root, file)
|
|
try:
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
if keyword not in content:
|
|
output_text.insert(
|
|
tk.END, f"'{keyword}' 未在文件 {file_path} 中找到。\n"
|
|
)
|
|
except Exception as e:
|
|
output_text.insert(tk.END, f"读取文件 {file_path} 出错: {e}\n")
|
|
output_text.insert(tk.END, "查找未包含关键字的文件完成。\n")
|
|
|
|
|
|
# 替换文件内容中的指定字符串
|
|
def replace_in_files(directory, search_text, replace_text, output_text):
|
|
if not search_text or not replace_text:
|
|
output_text.insert(tk.END, "请输入要替换的文本和新文本。\n")
|
|
return
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
for file in files:
|
|
if file.endswith(
|
|
(".js", ".json", ".vue", ".ts", ".py", ".java", ".cpp", ".xml")
|
|
):
|
|
file_path = os.path.join(root, file)
|
|
try:
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
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"在 {file_path} 中替换 '{search_text}' 为 '{replace_text}'\n",
|
|
)
|
|
except Exception as e:
|
|
output_text.insert(tk.END, f"处理文件 {file_path} 时出错: {e}\n")
|
|
output_text.insert(tk.END, "替换完成。\n")
|
|
|
|
|
|
# 转换指定颜色为透明色的功能
|
|
def convert_color_to_transparent(image_path, color, output_text):
|
|
img = Image.open(image_path)
|
|
img = img.convert("RGBA")
|
|
datas = img.getdata()
|
|
|
|
newData = []
|
|
for item in datas:
|
|
if item[0:3] == color:
|
|
newData.append((255, 255, 255, 0)) # 将指定颜色变为透明
|
|
else:
|
|
newData.append(item)
|
|
|
|
img.putdata(newData)
|
|
new_path = os.path.splitext(image_path)[0] + "_transparent.png"
|
|
img.save(new_path)
|
|
output_text.insert(tk.END, f"处理后的图片保存为 '{new_path}'\n")
|
|
|
|
|
|
# 清空输出框的函数
|
|
def clear_output(output_text):
|
|
output_text.delete(1.0, tk.END)
|
|
|
|
|
|
# 添加右键菜单
|
|
def add_right_click_menu(output_text):
|
|
menu = Menu(output_text, tearoff=0)
|
|
menu.add_command(label="清空输出", command=lambda: clear_output(output_text))
|
|
|
|
def show_menu(event):
|
|
menu.post(event.x_root, event.y_root)
|
|
|
|
output_text.bind("<Button-3>", show_menu)
|
|
|
|
|
|
# 创建GUI界面
|
|
def create_gui():
|
|
root = tk.Tk()
|
|
root.title("文件和图片处理工具")
|
|
root.geometry("800x500")
|
|
|
|
notebook = ttk.Notebook(root)
|
|
notebook.pack(fill="both", expand=True)
|
|
|
|
search_frame = ttk.Frame(notebook)
|
|
notebook.add(search_frame, text="搜索和替换")
|
|
image_frame = ttk.Frame(notebook)
|
|
notebook.add(image_frame, text="图片透明化")
|
|
note_frame = ttk.Frame(notebook)
|
|
notebook.add(note_frame, text="记事本")
|
|
|
|
# 搜索和替换界面
|
|
left_frame_search = ttk.Frame(search_frame)
|
|
left_frame_search.pack(side=tk.LEFT, fill=tk.Y, padx=10, pady=10)
|
|
|
|
keyword_entry = ttk.Entry(left_frame_search)
|
|
keyword_entry.pack(pady=5)
|
|
ttk.Label(left_frame_search, text="请输入要搜索的关键字:").pack(pady=5)
|
|
|
|
search_button = ttk.Button(
|
|
left_frame_search,
|
|
text="搜索文件夹",
|
|
command=lambda: browse_directory_for_search(
|
|
keyword_entry.get(), output_text_search
|
|
),
|
|
)
|
|
search_button.pack(pady=5)
|
|
|
|
search_text_entry = ttk.Entry(left_frame_search)
|
|
search_text_entry.pack(pady=5)
|
|
ttk.Label(left_frame_search, text="请输入要替换的文本:").pack(pady=5)
|
|
|
|
replace_text_entry = ttk.Entry(left_frame_search)
|
|
replace_text_entry.pack(pady=5)
|
|
ttk.Label(left_frame_search, text="请输入新文本:").pack(pady=5)
|
|
|
|
replace_button = ttk.Button(
|
|
left_frame_search,
|
|
text="替换文件夹中的内容",
|
|
command=lambda: browse_directory_for_replace(
|
|
search_text_entry.get(), replace_text_entry.get(), output_text_search
|
|
),
|
|
)
|
|
replace_button.pack(pady=5)
|
|
|
|
search_missing_button = ttk.Button(
|
|
left_frame_search,
|
|
text="查找不包含关键字的文件",
|
|
command=lambda: browse_directory_for_missing(
|
|
keyword_entry.get(), output_text_search
|
|
),
|
|
)
|
|
search_missing_button.pack(pady=5)
|
|
|
|
output_text_search = scrolledtext.ScrolledText(search_frame, width=70)
|
|
output_text_search.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
|
|
add_right_click_menu(output_text_search) # 为搜索输出框添加右键菜单
|
|
|
|
# 图像透明化界面
|
|
color_entry = ttk.Entry(image_frame)
|
|
color_entry.pack(pady=5)
|
|
ttk.Label(image_frame, text="请输入要透明化的颜色 (r,g,b):").pack(pady=5)
|
|
|
|
trans_button = ttk.Button(
|
|
image_frame,
|
|
text="使图片透明",
|
|
command=lambda: browse_image_for_transparency(
|
|
tuple(map(int, color_entry.get().split(","))), output_text_image
|
|
),
|
|
)
|
|
trans_button.pack(pady=5)
|
|
|
|
output_text_image = scrolledtext.ScrolledText(image_frame, width=70)
|
|
output_text_image.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
|
|
add_right_click_menu(output_text_image) # 为图片处理输出框添加右键菜单
|
|
|
|
# 记事本界面
|
|
note_text = scrolledtext.ScrolledText(note_frame, width=70)
|
|
note_text.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
|
|
|
|
save_button = ttk.Button(
|
|
note_frame, text="保存记事本", command=lambda: save_notes(note_text)
|
|
)
|
|
save_button.pack(pady=10)
|
|
|
|
root.mainloop()
|
|
|
|
|
|
# 辅助函数
|
|
def browse_directory_for_search(keyword, output_text):
|
|
directory = filedialog.askdirectory()
|
|
if directory:
|
|
output_text.insert(tk.END, f"正在 {directory} 中搜索关键字: '{keyword}'\n")
|
|
search_keyword_in_files(directory, keyword, output_text)
|
|
|
|
|
|
def browse_directory_for_replace(search_text, replace_text, output_text):
|
|
directory = filedialog.askdirectory()
|
|
if directory:
|
|
output_text.insert(
|
|
tk.END, f"在 {directory} 中将 '{search_text}' 替换为 '{replace_text}'\n"
|
|
)
|
|
replace_in_files(directory, search_text, replace_text, output_text)
|
|
|
|
|
|
def browse_directory_for_missing(keyword, output_text):
|
|
directory = filedialog.askdirectory()
|
|
if directory:
|
|
output_text.insert(
|
|
tk.END, f"正在 {directory} 中查找未包含关键字 '{keyword}' 的文件。\n"
|
|
)
|
|
find_files_without_keyword(directory, keyword, output_text)
|
|
|
|
|
|
def browse_image_for_transparency(color, output_text):
|
|
image_path = filedialog.askopenfilename()
|
|
if image_path:
|
|
convert_color_to_transparent(image_path, color, output_text)
|
|
|
|
|
|
def save_notes(note_text):
|
|
file_path = filedialog.asksaveasfilename(defaultextension=".txt")
|
|
if file_path:
|
|
with open(file_path, "w") as file:
|
|
file.write(note_text.get(1.0, tk.END))
|
|
note_text.insert(tk.END, f"记事本已保存到 {file_path}\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
create_gui()
|