title: Pass selection Listbox topwindow to main window
body: <p>This is my first real Python project. I am currently developing a GUI in Tkinter that allows the user to select Tasks and CVs to automatically compile documents using standard predefined task and CV texts from a database.</p>
  
  <p>I have created two "Add" buttons in the main window to add Tasks and CVs that show a popup Listbox that allow the user to select the Tasks and CVs they want to have included in the commercial proposal. I have managed to create the popup window as a separate Class and it stores the selected Tasks in a list, but now I need to pass the list with the selected items to the Listbox in the main window when the user clicks the Select button in the popup window, but I cannot get my head around on how to do that.</p>
  
  <p>I have researched on different fora and watched a variety of Youtube videos, but all focus on entry popups or some sort.</p>
  
  <p>This is the code for the main window:</p>
  
  <pre><code>from tkinter import *
  from Add import *
  
  # make main window
  root = Tk()
  
  theLabel = Label(root, text="ProposalBuilder")
  theLabel.grid(row=0)
  
  # make frames
  taskFrame = Frame(root)
  taskFrame.grid(row=1, column=0)
  
  CVFrame = Frame(root)
  CVFrame.grid(row=1, column=1)
  
  buildFrame = Frame(root)
  buildFrame.grid(row=2, columnspan=2)
  
  # add labels to frames
  taskLabel = Label(taskFrame, text="Tasks")
  taskLabel.pack()
  
  CVLabel = Label(CVFrame, text="CVs")
  CVLabel.pack()
  
  # add listboxes to frames
  scrollTask = Scrollbar(taskFrame, orient=VERTICAL)
  listTask = Listbox(taskFrame, selectmode=MULTIPLE, yscrollcommand=scrollTask.set)
  scrollTask.config(command=listTask.yview)
  scrollTask.pack(side=RIGHT, fill=Y)
  listTask.pack()
  
  scrollCV = Scrollbar(CVFrame, orient=VERTICAL)
  listCV = Listbox(CVFrame, selectmode=MULTIPLE, yscrollcommand=scrollCV.set)
  scrollCV.config(command=listCV.yview)
  scrollCV.pack(side=RIGHT, fill=Y)
  listCV.pack()
  
  # add commands to buttons
  def addTask():
      taskBox = Add('C:\\Users\\204703\\ProposalBuilder\\Database')
      sel_test = taskBox.selection
  
  def addCV():
      CVBox = Add('C:\\Users\\204703\\ProposalBuilder\\Database')
  
  # add buttons to frames
  buttonAddTask = Button(taskFrame, text="Add", command=addTask)
  buttonAddTask.pack(fill=X)
  buttonDelTask = Button(taskFrame, text="Delete")
  buttonDelTask.pack(fill=X)
  buttonUpTask = Button(taskFrame, text="Up")
  buttonUpTask.pack(fill=X)
  buttonDownTask = Button(taskFrame, text="Down")
  buttonDownTask.pack(fill=X)
  
  buttonAddCV = Button(CVFrame, text="Add", command=addCV)
  buttonAddCV.pack(fill=X)
  buttonDelCV = Button(CVFrame, text="Delete")
  buttonDelCV.pack(fill=X)
  buttonUpCV = Button(CVFrame, text="Up")
  buttonUpCV.pack(fill=X)
  buttonDownCV = Button(CVFrame, text="Down")
  buttonDownCV.pack(fill=X)
  
  buttonBuild = Button(buildFrame, text="Build Proposal")
  buttonBuild.pack(side=RIGHT)
  
  root.mainloop()
  </code></pre>
  
  <p>This is the code for the separate class I created for the popup window:</p>
  
  <pre><code>from tkinter import*
  from os import *
  
  class Add:
      def __init__(self, path):
          # the slected tasks
          self.selection = []
          # make a frame
          top = Toplevel()
  
          # get file names from the directory (path) and save in list
          self.path = path
          self.dirList = listdir(self.path)
  
          # add listbox to frames and populate with file names
          self.scrollList = Scrollbar(top, orient=VERTICAL)
          self.listbox = Listbox(top, selectmode=MULTIPLE, yscrollcommand=self.scrollList.set)
          self.scrollList.config(command=self.listbox.yview)
          self.scrollList.pack(side=RIGHT, fill=Y)
          for item in self.dirList:
              self.listbox.insert(END,item)
          self.listbox.pack()
  
          # add buttons to frame
          self.selectButton = Button(top, text="Select", command=self.select)
          self.selectButton.pack()
  
          self.quitButton = Button(top, text="Quit", command=top.destroy)
          self.quitButton.pack()
  
      # identify selected rows and return a list with the selection
      def select(self):
          selectedRows = self.listbox.curselection()
          for item in selectedRows:
                  self.selection.append(self.dirList[item])
          print(self.selection)
          return self.selection
  </code></pre>