|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码不知道哪里有问题,改完名总是会少一位数
比如应该改成 张1233.txt 结果实际改成了 张123.txt
- import tkinter as tk
- from tkinter import filedialog
- import os
- from tkinter import messagebox
- # 创建窗口
- window = tk.Tk()
- # 设置窗口标题
- window.title("My Window")
- # 设置窗口大小
- window.geometry("520x500")
- # 计算居中位置
- screen_width = window.winfo_screenwidth()
- screen_height = window.winfo_screenheight()
- x = (screen_width - window.winfo_reqwidth()) / 2
- y = (screen_height - window.winfo_reqheight()) / 2
- # 设置窗口位置
- window.geometry("+%d+%d" % (x, y))
- # 创建顶部输入框
- top_entry = tk.Entry(window)
- top_entry.place(x=50, y=10, width=400,height=25)
- # 创建顶部按钮
- def choose_folder():
- default_dir = os.getcwd()
- folder_path = filedialog.askdirectory(initialdir=default_dir)
- top_entry.delete(0, tk.END)
- top_entry.insert(0, folder_path)
- top_button = tk.Button(window, text="选择目录", command=choose_folder)
- top_button.place(x=430, y=10)
- # 创建左侧多行输入框控件和滚动条
- left_text = tk.Text(window)
- left_text.place(x=10, y=120, anchor="w", width=480,height=150 )
- left_scrollbar = tk.Scrollbar(left_text)
- left_scrollbar.place(x=460, y=0, height=145)
- left_text.config(yscrollcommand=left_scrollbar.set, wrap="char")
- # 设置左侧多行输入框控件的大小
- left_text.config(width=30, height=10)
- left_scrollbar.config(command=left_text.yview)
- # 创建右侧多行输入框控件和滚动条
- #right_text.place(x=left_text.winfo_x() + left_text.winfo_width(), y=200, anchor="w")
- right_text = tk.Text(window)
- right_text.place(x=10, y=300, anchor="w", width=480,height=150)
- right_scrollbar = tk.Scrollbar(right_text )
- right_scrollbar.place(x=460, y=0, height=145)
- right_text.config(yscrollcommand=right_scrollbar.set, wrap="word")
- # 设置右侧多行输入框控件的大小
- right_text.config(width=30, height=10)
- right_scrollbar.config(command=right_text.yview)
- def rename_txt_files(folder_path, left_text, right_text):
- lines = left_text.get("1.0", "end").split("\n")
- for root, dirs, files in os.walk(folder_path):
- for file in files:
- if file.endswith(".txt") and any(line in file for line in lines):
- old_name = os.path.join(root, file)
- line_num = lines.index([line for line in lines if line in file][0])
- new_name = os.path.join(root, right_text.get(f"{line_num+1}.0", f"{line_num+1}.end-1c").strip() + ".txt")
- print(new_name)
- if os.path.exists(new_name):
- #messagebox.showwarning("警告", f"文件名 {new_name} 已存在!")
- print("警告", f"文件名 {new_name} 已存在!")
- else:
- try:
- os.rename(old_name, new_name)
- except Exception as e:
- print("错误", f"重命名文件 {old_name} 为 {new_name} 时出错:{e}")
- #messagebox.showerror("错误", f"重命名文件 {old_name} 为 {new_name} 时出错:{e}")
- # 创建输出按钮
- def output_lines():
- folder_path = top_entry.get()
- if not folder_path:
- messagebox.showwarning("警告", "您还没有选择目录!")
- return
-
- lines = left_text.get("1.0", "end").split("\n")
- lines2 = right_text.get("1.0", "end").split("\n")
- rename_txt_files(folder_path, left_text, right_text)
- print(left_text)
- print(right_text)
- #遍历文件夹
- output_button = tk.Button(window, text="输出每行内容", command=output_lines)
- output_button.place(x=210, y=390)
- # 运行窗口
- window.mainloop()
复制代码
对不起,我之前的解释可能有误,你在这里遇到的问题可能与获取新名称中的行号有关。你的代码在使用 "right_text.get(f"{line_num+1}.0", f"{line_num+1}.end-1c")" 时可能在某些情况下没有得到正确的结果,特别是当 right_text 中的行数少于 left_text 中的行数时。这可能导致你从 right_text 中获取的新文件名字符串比预期的短。
我认为可以更改代码以确保每次都能正确地从 right_text 中获取新文件名。以下是我对 rename_txt_files 函数的改动:
- def rename_txt_files(folder_path, left_text, right_text):
- lines = left_text.get("1.0", "end").split("\n")
- lines2 = right_text.get("1.0", "end").split("\n")
- for root, dirs, files in os.walk(folder_path):
- for file in files:
- if file.endswith(".txt") and any(line in file for line in lines):
- old_name = os.path.join(root, file)
- line_num = lines.index([line for line in lines if line in file][0])
- if line_num < len(lines2):
- new_name = os.path.join(root, lines2[line_num].strip() + ".txt")
- print(new_name)
- if os.path.exists(new_name):
- print("警告", f"文件名 {new_name} 已存在!")
- else:
- try:
- os.rename(old_name, new_name)
- except Exception as e:
- print("错误", f"重命名文件 {old_name} 为 {new_name} 时出错:{e}")
- else:
- print("错误", f"在右侧文本框中找不到对应的新文件名")
复制代码
在这个改动中,我首先将 right_text 的所有行读入一个名为 lines2 的列表中。然后,每次获取新的文件名时,我会检查 line_num 是否小于 lines2 的长度。如果是,我会使用 lines2[line_num] 作为新的文件名。如果不是,我会打印一个错误消息。这样可以确保我们不会试图从 right_text 中获取一个不存在的行。
希望这个改动能帮助你解决问题。如果仍然存在问题,请告诉我,我会尽力帮助你解决。
|
|