Skip to content

Instantly share code, notes, and snippets.

@mitechie
Created August 1, 2013 15:18
Show Gist options
  • Save mitechie/6132350 to your computer and use it in GitHub Desktop.
Save mitechie/6132350 to your computer and use it in GitHub Desktop.
This isn't run/tested but a general refactor. What = \ ?
from kivy.adapters.dictadapter import DictAdapter
from kivy.uix.gridlayout import GridLayout
from kivy.uix.listview import ListItemButton
from kivy.uix.listview import ListView
from fixtures import fruit_categories
from fixtures import fruit_data
from fruit_detail_view import FruitDetailView as DetailView
Fixtures = namedtuple('Fixtures', 'categories data')
DATA = Fixtures(fruit_categories, fruit_data)
# A custom adapter is needed here, because we must transform the selected
# fruit category into the list of fruit keys for that category.
class FruitDictAdapter(DictAdapter):
def on_change(self, adapter, *args):
if len(adapter.selection) == 0:
self.data = {}
return
category = DATA.categories[adapter.selection[0].text]
self.sorted_keys = category['fruits']
def args_converter(row_index, rec):
return {
'text': rec['name'],
'size_hint_y': None,
'height': 25
}
class CascadingView(GridLayout):
"""Implementation of a cascading style display
Provides a scrollable list of fruit categories on the left, a list of
fruits for the selected category in the middle, and a fruit detail view on
the right.
This examples uses :class:`DictAdapter`. See an equivalent treatment done
with :class:`ListAdapter` in list_cascade.py.
See list_cascade_images.py for the same example, also using
:class:`DictAdapter`, and with images of fruit in fruit list items and in
the detail view.
"""
def __init__(self, **kwargs):
kwargs['cols'] = 3
super(CascadingView, self).__init__(**kwargs)
# Fruit categories list on the left:
#
categories = sorted(fruit_categories.keys())
categories_dict_adapter = DictAdapter(sorted_keys=categories,
data=fruit_categories,
args_converter=args_converter,
selection_mode='single',
allow_empty_selection=False,
cls=ListItemButton)
categories_list_view = ListView(adapter=dict_adapter,
size_hint=(.2, 1.0))
self.add_widget(list_view)
fruits_dict_adapter = FruitsDictAdapter(
sorted_keys=DATA.categories[categories[0]]['fruits'],
data=DATA.data,
args_converter=args_converter,
selection_mode='single',
allow_empty_selection=False,
cls=ListItemButton)
dict_adapter.bind(on_selection_change=fruits_dict_adapert.on_change)
fruit_list_view = ListView(adapter=fruits_dict_adapter,
size_hint=(.2, 1.0))
self.add_widget(fruits_list_view)
# Detail view, for a given fruit, on the right:
#
detail_view = FruitDetailView(
fruit_name=fruits_dict_adapter.selection[0].text,
size_hint=(.6, 1.0))
fruits_dict_adapter.bind(on_selection_change=detail_view.on_change)
self.add_widget(detail_view)
if __name__ == '__main__':
from kivy.base import runTouchApp
runTouchApp(CascadingView(width=800))
@jacebrowning
Copy link

I can't say if it reads the same (not knowing exactly what the other code did), but it looks much better without the "= ".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment