1

I have a function that opens a top-level window, places a listbox widget, populates it, and then on close should store those selected values to a list, but I am getting an error that I can't seem to find in any google search. Any ideas?

def select_exer():
    #Create the new Window
    sel_exer_window = tk.Toplevel(main)
    sel_exer_window.geometry("+%d+%d" % (x+1150,y+180))

    #Descriptive Text
    exer_text = tk.Label(sel_exer_window, text="Select your exercises").pack()

    exercise_list = tk.Listbox(sel_exer_window, selectmode='multiple')
    exercise_list.pack()

    def poplist():
        lst = exer_options
        exercise_list.insert("end",*lst)

    poplist()

    def on_closing():
        #Unlocks the next Button
        COUNT_DOWN_BUTTON.config(state="normal")
        #Closes the Window
        sel_exer_window.destroy()

        #lst_var = [exercise_list.get(idx) for idx in exercise_list.curselection()]
        lst_var = [exercise_list.get(idx) for idx in exercise_list.curselection()]
        print(type(lst_var))

    sel_exer_window.protocol("WM_DELETE_WINDOW", on_closing)
jon
  • 349
  • 3
  • 4
  • 20
  • You know that `exer_text` is always `None`, right? For more info read [this](https://stackoverflow.com/a/66385069/11106801) – TheLizzard Mar 07 '21 at 19:02
  • I'm not sure what you mean? – jon Mar 07 '21 at 19:36
  • When you did `exer_text = tk.Label(...).pack()` it stores the result of the `.pack` (which is always `None`) in `exer_text` not the actual label widget. So what you actually wanted to do is: `exer_text = tk.Label(...)` and then `exer_text.pack()`. – TheLizzard Mar 07 '21 at 19:50
  • Oh, interesting. I wasn't interacting with the Label widget but that is good information to know! – jon Mar 07 '21 at 22:48

1 Answers1

1

You are calling sel_exer_window.destroy() which calls exercise_list.destroy() which destroys the Listbox. After that you are trying to get its values using lst_var = [exercise_list.get(idx) for idx in exercise_list.curselection()]. You can't get anything out of a destroyed widget. I suggest you change that to:

def on_closing():
    #Unlocks the next Button
    COUNT_DOWN_BUTTON.config(state="normal")

    #lst_var = [exercise_list.get(idx) for idx in exercise_list.curselection()]
    lst_var = [exercise_list.get(idx) for idx in exercise_list.curselection()]
    print(type(lst_var))

    #Closes the Window
    sel_exer_window.destroy()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31