更新 barcode_plus_dot_1.py
This commit is contained in:
@@ -1,58 +1,58 @@
|
|||||||
import os
|
import os
|
||||||
from tkinter import Tk, Button, Label, filedialog, messagebox
|
from tkinter import Tk, Button, Label, filedialog, messagebox
|
||||||
|
|
||||||
class BarcodeProcessor:
|
class BarcodeProcessor:
|
||||||
def __init__(self, root):
|
def __init__(self, root):
|
||||||
self.root = root
|
self.root = root
|
||||||
self.root.title("条码处理程序 V0.1.1.20250109 @BLTC")
|
self.root.title("条码处理程序 V0.1.1.20250109 @BLTC")
|
||||||
self.root.geometry("400x150")
|
self.root.geometry("400x150")
|
||||||
|
|
||||||
# 创建界面元素
|
# 创建界面元素
|
||||||
self.label = Label(root, text="选择要处理的txt文件")
|
self.label = Label(root, text="选择要处理的txt文件")
|
||||||
self.label.pack(pady=10)
|
self.label.pack(pady=10)
|
||||||
|
|
||||||
self.select_button = Button(root, text="选择文件", command=self.select_files)
|
self.select_button = Button(root, text="选择文件", command=self.select_files)
|
||||||
self.select_button.pack(pady=5)
|
self.select_button.pack(pady=5)
|
||||||
|
|
||||||
self.process_button = Button(root, text="开始处理", command=self.process_files, state="disabled")
|
self.process_button = Button(root, text="开始处理", command=self.process_files, state="disabled")
|
||||||
self.process_button.pack(pady=5)
|
self.process_button.pack(pady=5)
|
||||||
|
|
||||||
self.files = []
|
self.files = []
|
||||||
|
|
||||||
def select_files(self):
|
def select_files(self):
|
||||||
self.files = filedialog.askopenfilenames(
|
self.files = filedialog.askopenfilenames(
|
||||||
title="选择要处理的txt文件",
|
title="选择要处理的txt文件",
|
||||||
filetypes=[("Text files", "*.txt")],
|
filetypes=[("Text files", "*.txt")],
|
||||||
initialdir=os.getcwd()
|
initialdir=os.getcwd()
|
||||||
)
|
)
|
||||||
if self.files:
|
if self.files:
|
||||||
self.process_button.config(state="normal")
|
self.process_button.config(state="normal")
|
||||||
self.label.config(text=f"已选择 {len(self.files)} 个文件")
|
self.label.config(text=f"已选择 {len(self.files)} 个文件")
|
||||||
|
|
||||||
def process_files(self):
|
def process_files(self):
|
||||||
for file in self.files:
|
for file in self.files:
|
||||||
try:
|
try:
|
||||||
with open(file, 'r') as f:
|
with open(file, 'r') as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
|
||||||
modified_lines = []
|
modified_lines = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
stripped_line = line.strip()
|
stripped_line = line.strip()
|
||||||
if not stripped_line.endswith(",1"):
|
if not stripped_line.endswith(",1"):
|
||||||
modified_lines.append(stripped_line + ",1")
|
modified_lines.append(stripped_line + ",1")
|
||||||
else:
|
else:
|
||||||
modified_lines.append(stripped_line)
|
modified_lines.append(stripped_line)
|
||||||
|
|
||||||
with open(file, 'w') as f:
|
with open(file, 'w') as f:
|
||||||
f.write('\n'.join(modified_lines))
|
f.write('\n'.join(modified_lines))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
messagebox.showerror("错误", f"处理文件 {file} 时出错:\n{str(e)}")
|
messagebox.showerror("错误", f"处理文件 {file} 时出错:\n{str(e)}")
|
||||||
return
|
return
|
||||||
|
|
||||||
messagebox.showinfo("完成", "文件处理完成!")
|
messagebox.showinfo("完成", "文件处理完成!")
|
||||||
self.root.destroy()
|
self.root.destroy()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
root = Tk()
|
root = Tk()
|
||||||
app = BarcodeProcessor(root)
|
app = BarcodeProcessor(root)
|
||||||
root.mainloop()
|
root.mainloop()
|
||||||
Reference in New Issue
Block a user