[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinterdnd2 import TkinterDnD, DND_FILES
def read_file(file_path):
# 尝试使用不同的编码读取文件
for encoding in ['utf-8', 'ansi']:
try:
with open(file_path, 'r', encoding=encoding) as file:
return [line.strip() for line in file]
except (UnicodeDecodeError, LookupError):
continue
raise ValueError("无法读取文件,文件编码不受支持")
def replace_track_names(cue_content, song_names):
for i, song_name in enumerate(song_names, start=1):
old_track = f"音轨{i:02}"
new_track = song_name
cue_content = cue_content.replace(old_track, new_track)
return cue_content
def process_files():
song_file_path = song_file_entry.get()
cue_file_path = cue_file_entry.get()
if not song_file_path or not cue_file_path:
messagebox.showerror("错误", "请选择文件")
return
try:
song_names = read_file(song_file_path)
# 尝试以不同编码读取CUE文件
cue_content = None
for encoding in ['utf-8', 'ansi']:
try:
with open(cue_file_path, 'r', encoding=encoding) as cue_file:
cue_content = cue_file.read()
break
except (UnicodeDecodeError, LookupError):
continue
if cue_content is None:
raise ValueError("无法读取CUE文件,文件编码不受支持")
new_cue_content = replace_track_names(cue_content, song_names)
with open(cue_file_path, 'w', encoding='utf-8') as cue_file:
cue_file.write(new_cue_content)
messagebox.showinfo("成功", "文件处理完成")
except Exception as e:
messagebox.showerror("错误", f"处理文件时出错: {e}")
def browse_song_file():
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
song_file_entry.delete(0, tk.END)
song_file_entry.insert(0, file_path)
def browse_cue_file():
file_path = filedialog.askopenfilename(filetypes=[("CUE files", "*.cue")])
cue_file_entry.delete(0, tk.END)
cue_file_entry.insert(0, file_path)
def drop_song_file(event):
song_file_entry.delete(0, tk.END)
song_file_entry.insert(0, event.data)
def drop_cue_file(event):
cue_file_entry.delete(0, tk.END)
cue_file_entry.insert(0, event.data)
# 创建主窗口
root = TkinterDnD.Tk()
root.title("CUE文件音轨替换工具")
# 创建并放置标签和输入框
tk.Label(root, text="歌曲名列表文件:").grid(row=0, column=0, padx=10, pady=10)
song_file_entry = tk.Entry(root, width=50)
song_file_entry.grid(row=0, column=1, padx=10, pady=10)
song_file_entry.drop_target_register(DND_FILES)
song_file_entry.dnd_bind('<<Drop>>', drop_song_file)
tk.Button(root, text="浏览", command=browse_song_file).grid(row=0, column=2, padx=10, pady=10)
tk.Label(root, text="CUE文件:").grid(row=1, column=0, padx=10, pady=10)
cue_file_entry = tk.Entry(root, width=50)
cue_file_entry.grid(row=1, column=1, padx=10, pady=10)
cue_file_entry.drop_target_register(DND_FILES)
cue_file_entry.dnd_bind('<<Drop>>', drop_cue_file)
tk.Button(root, text="浏览", command=browse_cue_file).grid(row=1, column=2, padx=10, pady=10)
# 创建并放置处理按钮
tk.Button(root, text="处理文件", command=process_files).grid(row=2, column=1, padx=10, pady=20)
# 运行主循环
root.mainloop()
''''''
''''''
''''''
'''
def clean_path(dropped_data):
# 移除路径中可能存在的花括号
return dropped_data.strip("{}")
def drop_song_file(event):
song_file_entry.delete(0, tk.END)
file_path = clean_path(event.data)
song_file_entry.insert(0, file_path)
def drop_cue_file(event):
cue_file_entry.delete(0, tk.END)
file_path = clean_path(event.data)
cue_file_entry.insert(0, file_path)
# 创建主窗口
root = TkinterDnD.Tk()
root.title("CUE文件音轨替换工具")
# 创建并放置标签和输入框
tk.Label(root, text="歌曲名列表文件:").grid(row=0, column=0, padx=10, pady=10)
song_file_entry = tk.Entry(root, width=50)
song_file_entry.grid(row=0, column=1, padx=10, pady=10)
song_file_entry.drop_target_register(DND_FILES)
song_file_entry.dnd_bind('<<Drop>>', drop_song_file)
tk.Button(root, text="浏览", command=browse_song_file).grid(row=0, column=2, padx=10, pady=10)
tk.Label(root, text="CUE文件:").grid(row=1, column=0, padx=10, pady=10)
cue_file_entry = tk.Entry(root, width=50)
cue_file_entry.grid(row=1, column=1, padx=10, pady=10)
cue_file_entry.drop_target_register(DND_FILES)
cue_file_entry.dnd_bind('<<Drop>>', drop_cue_file)
tk.Button(root, text="浏览", command=browse_cue_file).grid(row=1, column=2, padx=10, pady=10)
# 创建并放置处理按钮
tk.Button(root, text="处理文件", command=process_files).grid(row=2, column=1, padx=10, pady=20)
# 运行主循环
root.mainloop()
'''