# coding=utf-8
import tkinter as tk
from tkinter import filedialog, messagebox
from decimal import Decimal
'''打开选择文件夹对话框'''
window = tk.Tk()
window.title("规划坐标合并1.0 cby: 放放风")
window.geometry('790x465+500+200') # 290 160为窗口大小,+1000 +10 定义窗口弹出时的默认展示位置
window['background'] = 'Pink'
v = tk.StringVar()
listbox = tk.Listbox(window, height=20, width=90, listvariable=v)
#
# 处理单击选项
def show(event):
# nearest可以传回最接近y坐标在Listbox的索引
# 传回目前选项的索引
listbox.index = listbox.nearest(event.y)
# 处理拖拽选项
def showInfo(event):
# 获取目前选项的新索引
newIndex = listbox.nearest(event.y)
# 判断,如果向上拖拽
if newIndex < listbox.index:
# 获取新位置的内容
x = listbox.get(newIndex)
# 删除新内容
listbox.delete(newIndex)
# 将新内容插入,相当于插入我们移动后的位置
listbox.insert(newIndex + 1, x)
# 把需要移动的索引值变成我们所希望的索引,达到了移动的目的
listbox.index = newIndex
elif newIndex > listbox.index:
# 获取新位置的内容
x = listbox.get(newIndex)
# 删除新内容
listbox.delete(newIndex)
# 将新内容插入,相当于插入我们移动后的位置
listbox.insert(newIndex - 1, x)
# 把需要移动的索引值变成我们所希望的索引,达到了移动的目的
listbox.index = newIndex
def listbox_delete():
listbox.delete(0, tk.END)
#
# 查找列表中指定分隔符所有位置,返回列表-------------------------------------------
def text_pos(stra, delimiter):
ly = [-1]
for x in range(stra.count(delimiter)):
ly.append(stra.index(delimiter, ly[-1] + 1))
return ly[1:]
# 清除文本中的空格和回车--------------------------------------------------------
def filter_txt(patchw):
new_listw = []
charw = ""
with open(patchw) as f:
contentsw = f.readlines()
for linew in contentsw:
for xw in linew:
if xw != " ":
charw += xw
new_listw.append(charw)
charw = ""
filter_listw = [w for w in [r.replace("\n", "") for r in new_listw] if w != '']
return filter_listw
# 返回保存文件名 ------------------------------------------------------------
def fileSave():
filenewpath = filedialog.asksaveasfilename(defaultextension='.txt', filetypes=[("TXT", ".txt")],
title="请选择要保存的位置") # 设置保存文件,并返回文件名
return filenewpath
# 不加40-----------------------------------------------------------------------
def txt_merge():
Folderpath = filedialog.askopenfilenames(filetypes=[("文本", "txt")], title="请选择需要合并的规划文件")
if not Folderpath:
tk.messagebox.showinfo('警告', "请选择需要合并的规划坐标")
return Folderpath
#
def list_save(): # 选择需要处理的文件,循环插入路径到列表框
for x in txt_merge():
listbox.insert(tk.END, x)
listbox.bind('<Button-1>', show)
listbox.bind('<B1-Motion>', showInfo)
def txt_merge3():
plan = open(fileSave(), 'w') # 打开保存文件的文件
plan_list = open("list.txt", 'w') # 打开list.txt,没有创建
lxo = v.get().split("'")
xxo = [xxj for xxj in lxo if len(xxj) > 3]
n0, n1 = 0, 0
for filepath in xxo: # 循环选择的文件路径
n1 += 1
for line in filter_txt(filepath): # filter_txt清除文件中的空格和回车,返回列表。循环列表
n0 += 1
str_modify_list1 = line.split(",")
str_modify_list1[0] = str(n0)
str_modify_list1[1] = str(n1)
str_modify_list_hebing1 = ",".join(str_modify_list1)
plan.writelines(str_modify_list_hebing1)
plan.write('\n')
plan_list.write(filepath + "\n")
plan.close()
plan_list.close()
tk.messagebox.showinfo('结果', "合并成功\n\n合并顺序保存在:list.txt")
return n1
# 加40-----------------------------------------------------------------------------
def txt_merge2():
m0, m1 = 0, 0
plan2 = open(fileSave(), 'w') # 打开保存文件的文件
plan_list2 = open('list+40.txt', 'w') # 打开result.txt,没有创建
lxo = v.get().split("'")
xxo = [xxj for xxj in lxo if len(xxj) > 3]
for filepath2 in xxo:
m1 += 1
for line2 in filter_txt(filepath2):
m0 += 1
str_modify_list = line2.split(",")
str_modify_list[0] = str(m0)
str_modify_list[1] = str(m1)
str_modify_list[5] = str(40000000 + Decimal(str_modify_list[5]))
str_modify_list_hebing = ",".join(str_modify_list)
plan2.writelines(str_modify_list_hebing)
plan2.write('\n')
plan_list2.write(filepath2 + "\n")
plan2.close()
plan_list2.close()
tk.messagebox.showinfo('结果', "合并成功\n\n合并顺序保存在:list+40.txt")
return m0
# -------------------------------------------------------------------------------
cjip = tk.PhotoImage(file="D:\\a5.gif")
listbox.grid(row=0, column=0, rowspan=4, columnspan=5)
tk.Button(window, width=20, height=5, text="打开文件", command=list_save, bg="Yellow").grid(row=0, column=6)
tk.Button(window, width=20, height=5, text="合并", command=txt_merge3, bg="Lawngreen").grid(row=2, column=6)
tk.Button(window, width=20, height=5, text="(+40)合并", command=txt_merge2, bg="Deepskyblue").grid(row=3, column=6)
tk.Label(window, image=cjip, height=94, width=180).grid(row=4, column=1)
tk.Button(window, text="清空列表", width=20, height=5, command=listbox_delete, bg="Red").grid(row=4, column=4)
window.mainloop()
规划坐标合并
最新推荐文章于 2024-07-27 12:20:46 发布