Thank you for all the help! I have a solution below. I used a dictionary but stored the file as key and path as value. This allowed me to get around having duplicate paths. I also used the replace function to convert the path into the proper format.
I will look into the proposed solution of using ttk.Treeview to see if this is a better solution but as of now my program is working as hoped!
Thanks again!
import tkinter as tk
from tkinter import BOTH, LEFT, Button, Frame, Menu, Toplevel, filedialog
import os
ws = tk.Tk()
ws.title('Select Files to Import')
ws.geometry('500x200')
ws.config(bg='#456')
f = ('sans-serif', 13)
btn_font = ('sans-serif', 10)
bgcolor = '#BF5517'
dict = {}
def sort():
global second
second = Toplevel()
second.geometry('800x400')
menubar=Menu(second)
menubar.add_command(label="Save List", command=save)
menubar.add_command(label="Add Files", command=add)
second.config(menu=menubar)
global listbox
listbox = Drag_and_Drop_Listbox(second)
listbox.pack(fill=tk.BOTH, expand=True)
directory = filedialog.askopenfilenames()
n = 0
for file in directory:
key = os.path.basename(file)
value = os.path.dirname(file)
update_dict(key, value)
listbox.insert(n, os.path.basename(file))
n=n+1
def update_dict(key, value):
if key in dict:
dict[key].append(value)
else:
dict[key] = [value]
def add():
directory = filedialog.askopenfilenames()
n = 0
for file in directory:
key = os.path.basename(file)
value = os.path.dirname(file)
update_dict(key, value)
listbox.insert(n, os.path.basename(file))
n=n+1
def save():
image=listbox.get(0, listbox.size())
for x in image:
string = str(dict[x]).replace('[','').replace(']','').replace('/','\\').replace('\'','')
print(string + '\\' + x)
class Drag_and_Drop_Listbox(tk.Listbox):
def __init__(self, master, **kw):
kw['selectmode'] = tk.SINGLE
kw['activestyle'] = 'none'
tk.Listbox.__init__(self, master, kw)
self.bind('<Button-1>', self.getState, add='+')
self.bind('<Button-1>', self.setCurrent, add='+')
self.bind('<B1-Motion>', self.shiftSelection)
def setCurrent(self, event):
self.curIndex = self.nearest(event.y)
def getState(self, event):
i = self.nearest(event.y)
self.curState = self.selection_includes(i)
def shiftSelection(self, event):
i = self.nearest(event.y)
if self.curState == 1:
self.selection_set(self.curIndex)
else:
self.selection_clear(self.curIndex)
if i < self.curIndex:
x = self.get(i)
selected = self.selection_includes(i)
self.delete(i)
self.insert(i+1, x)
if selected:
self.selection_set(i+1)
self.curIndex = i
elif i > self.curIndex:
x = self.get(i)
selected = self.selection_includes(i)
self.delete(i)
self.insert(i-1, x)
if selected:
self.selection_set(i-1)
self.curIndex = i
frame = Frame(ws, padx=20, pady=20, bg=bgcolor)
frame.pack(expand=True, fill=BOTH)
btn_frame = Frame(frame, bg=bgcolor)
btn_frame.grid(columnspan=2, pady=(50, 0))
sort_btn = Button(
btn_frame,
text='Individual Sort',
command=sort,
font=btn_font,
padx=10,
pady=5
)
sort_btn.pack(side=LEFT, expand=True, padx=(5,5))
# mainloop
ws.mainloop()