How do you fill the items of a DropDown from a data table?

That’s right - the items property of a DropDown must be an iterable object containing strings or 2-tuples. If they’re tuples, the first element of the tuple is the text displayed for that option in the DropDown, and the second element is what you’ll get from the selected_value property if that option is selected. (See the docs here.)

app_tables.todo.search() gives you an iterable object containing rows from the data table. Rows are a bit like dictionaries - you can access column values using square brackets, eg row['description'].

So if you have a text column in your table called 'description', you can turn a list of rows into a list of strings with a list comprehension:

self.dropdown_description.items = [row['description'] for row in app_tables.todo.search()]

Then, you can get the selected description string by reading self.dropdown_description.selected_value.

But what if you didn’t want the string? You want the dropdown box to show the description text, but you want to access the row object itself (eg so you can access other columns on that row). That’s where you use a tuple:

self.dropdown_descriptions.items = [(row['description'], row) for row in app_tables.todo.search()]

Now, self.dropdown_descriptions.selected_value will be a row from the table. So if you had another column called 'done', you could mark the selected row as done with code like this:

selected_row = self.dropdown_descriptions.selected_value
selected_row['done'] = True

Does that help explain it?

3 Likes