You are very close. You can no longer use tkinter widget methods after a window is destroyed, but you can use your own methods and attributes:
import tkinter as tk
class MainWin(tk.Tk):
def __init__(self):
super().__init__()
tk.Button(self, text='click me', command=self.dia).pack()
self.geometry('200x200')
def dia(self):
TL = dialogWin(self)
MainWin.wait_window(TL)
print(TL.choice)
class dialogWin(tk.Toplevel):
def __init__(self, MainWin):
super().__init__(MainWin)
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')
self.LB.grid()
for x in range(10):
self.LB.insert(tk.END, f"this is item {x}")
def getChoices(self):
self.choice = self.LB.curselection() # <== save the choice as an attribute
self.destroy()
MainWin().mainloop()
You could also save it as an attribute of the main window if you wanted to:
def getChoices(self):
self.master.choice = self.LB.curselection()
self.destroy()
Or any other place really.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…