pythonlisttkinterlistboxiteration

Tkinter: Listbox not populating from function call, populates from list


I have a function that creates a list, and another that's supposed to populate a tkinter listbox with the items from that list. When a radio button is clicked the contents of the listbox should update.

The function to create the list -- get_available_decks() -- works as expected on the command line. But the listbox is not populating from that list.

When I replace the call to get_available_decks with a test list such as ['1', '2', '3'], the listbox populates as expected.

Why isn't the listbox populating from the call to get_available_decks?

def get_available_decks(master_deck_list, deck_type_selection):
    # Assign the list of deck names to deck_list
    decks_list = []
    
    for av_deck in master_deck_list:
        if deck_type_selection == 'official':
            if av_deck['official'] == True:
                decks_list.append(av_deck['name'])
        elif deck_type_selection == 'community':
            if av_deck['official'] == False:
                decks_list.append(av_deck['name'])
        elif deck_type_selection == 'all':
            decks_list.append(av_deck['name'])

    # for i in decks_list:
    #     print(i)

    return decks_list

def update_decklist():
    av_box.delete(0, tk.END)
    decklist = get_available_decks(master_deck_list, deck_type_selection)
    for item in decklist:
        # item = tk.StringVar(item)
        av_box.insert(tk.END, item)

# Deck type radio buttons
radio_1 = tk.Radiobutton(decktypes_frame, text="Official", variable=deck_type_selection, value="official", command=lambda: update_decklist())
radio_1.grid(column=0, row=0, padx=5, pady=5)
radio_2 = tk.Radiobutton(decktypes_frame, text="Community", variable=deck_type_selection, value="community", command=lambda: update_decklist())
radio_2.grid(column=1, row=0, padx=5, pady=5)
radio_3 = tk.Radiobutton(decktypes_frame, text="All", variable=deck_type_selection, value="all", command=lambda: update_decklist())
radio_1.grid(column=0, row=0, padx=5, pady=5)
radio_3.grid(column=2, row=0, padx=5, pady=5)

# decksframe holds the available and selected decks lists
decksframe = tk.LabelFrame(main, text="Available Decks")
decksframe.columnconfigure(0, weight=3)
decksframe.columnconfigure(1, weight=3)

# Available Decks
av_box = tk.Listbox(decksframe, selectmode="multiple", exportselection=0)
av_box.grid(column=0, row=0, padx=5, pady=5)

Solution

  • Note that deck_type_selection passed to get_available_decks() is an instance of tkinter StringVar, so checking it with a string inside get_available_decks() will most likely be evaluated as False and nothing will be appended into decks_list. You should pass the value in that variable instead:

    def update_decklist():
        av_box.delete(0, tk.END)
        # use .get() to get the value of deck_type_selection
        decklist = get_available_decks(master_deck_list, deck_type_selection.get())
        for item in decklist:
            av_box.insert(tk.END, item)