I'm writing a portion of code which is supposed to behave the following way:
- Display a listbox of items.
- The user selects one or more items from the listbox.
- The user clicks a seperate button widget to confirm the selection.
- The toplevel window closes once the user clicked the button.
- The selection is saved so that other portions of the program can access it.
I have the following code (code not relevant to the question is omitted):
class MainWin(tk.Tk):
# ... omitted code
def dia(self):
'''
This creates a toplevel window that displays the listbox.
'''
TL = dialogWin(self)
MainWin.wait_window(TL)
class dialogWin(tk.Toplevel):
'''
This window creates and displays the listbox.
'''
def __init__(self, MainWin):
# ... omitted code
# ...
B = tk.Button(self, text = 'Confirm', command = self.getChoices).grid(row = 3) #This is the button that the user clicks to confirm the selection
#create listbox
self.LB = tk.Listbox(self, height = 10, width = 45, selectmode = 'multiple')
# ... code to configure the listbox appearence
def getChoices(self):
'''
This is the callback function the listbox uses.
'''
choice = self.LB.curselection()
self.destroy()
return choice
That's the code that I have but I can't seem to find a way to actually access the information stored in choice. For instance if I edit dia(self) to confirm the return value, like so:
def dia(self):
TL = dialogWin(self)
MainWin.wait_window(TL)
print(TL.getChoices())
I get the error _tkinter.TclError: invalid command name ".!dialogwin.!listbox". TL.getChoices().get() gives me the same error. What I want is the selected information to be saved as something like (1, 2, 3) where I can then access the tuple from the main window. Is there something I've done wrong here?