pfalcon / picotui

Lightweight, pure-Python Text User Interface (TUI) widget toolkit with minimal dependencies. Dedicated to the Pycopy project.
https://github.com/pfalcon/pycopy
MIT License
811 stars 70 forks source link

ListBox.items on changed #16

Closed peterjschroeder closed 7 years ago

peterjschroeder commented 7 years ago

Modifying the items in a ListBox, even when calling a changed and redraw on it doesn't update the items in the list.

Example:

Create a DropDown list to filter the ListBox

    d.add(2, 3, "Manufacturer:")
    mfrs = list(set([figures[i][4] for i in range(0,len(figures))]))
    mfrs.sort()
    mfrs.insert(0, ('All',))
    filters_mfrs = WDropDown(20, ["%s" % items for items in mfrs], dropdown_h=round(cheight/4))
    d.add(16, 3, filters_mfrs)

Create the ListBox

    figure_list = WListBox(round(cwidth/2.26), cheight-11, ["%s" % items[0] for items in figures])
    d.add(2, 9, figure_list)

When the DropDown is changed, filter the ListBox based on the selection

    def filters_changed(w):
        ffigures = []
        for i in range(0, len(figures)):
            if w.items[w.choice] == "All" or figures[i][4] == w.items[w.choice]:
                ffigures.append(figures[i])
        figure_list.items = [ffigures[i][6] for i in range(0,len(ffigures))][0]
        figure_list.redraw()
    filters_mfrs.on("changed", filters_changed)
peterjschroeder commented 7 years ago

I figured it out. Besides setting items, set_lines must also be set to the new items.

It looks like ListBox could use a custom redraw function like some of the rest of them. The cursor isn't reset either which causes the new ListBox list to error out if the cursor was on a higher line than the bottom line of the new. If I try to reset the cursor manually by setting to 0, it will work some times but still errors sometimes.

peterjschroeder commented 7 years ago

By setting top_line, cur_line, and row to 0, this allows a Listbox to be updated and not crash when new items are set.