0

I am using the Kivy library with python to develop an app.

In the app I have two screens which are almost identical: they both contain lists and they have roughly the same layout (row of buttons above, then list, then row of buttons below).

The biggest difference is the number of buttons on each screen: Screen 1 is 4 buttons across, and screen 2 is 3 buttons across. The buttons will have different behaviors and text depending on the screen.

Is there a way to use the same Kivy definition (sample from screen 1 shown below) but dynamically change what buttons are added when I declare the screens?

EDIT: Added sample code from my python file as well, see below the Kivy sample.

#:kivy 1.8.0
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
<ListScreen>:
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            size_hint_y: .1
            Button:
                text: 'Save'
            Button:
                text: 'Load'
            Button:
                text: 'New'
            Button:
                text: 'New Search'
        ListView:
            size_hint_y: .8
            adapter:
                ListAdapter(data=root.itemList,
                selection_mode='single',
                allow_empty_selection=False,
                cls=ListItemButton)
            canvas.before:
                Color:
                    rgb: 1,1,1
                Rectangle:
                    pos: self.pos
                    size: self.size
        BoxLayout:
            size_hint_y: .1
            Button:
                text: 'Add'
            Button:
                text: 'Remove'
            Button:
                text: 'View'
            Button:
                text: 'View Search'
                on_press:
                    root.manager.transition.direction = 'left'
                    root.manager.current = 'results'

class ListScreen(Screen):
    itemList = ListProperty([])
    selected_value = StringProperty()

    def change(self,change):
        self.selected_value = 'Selected: {}'.format(change.text)

sm = ScreenManager()
sm.add_widget(ListScreen(name='list'))
sm.add_widget(ListScreen(name='results'))
boardrider
  • 5,882
  • 7
  • 49
  • 86
jdanaher
  • 207
  • 1
  • 3
  • 11
  • You could [disable](http://stackoverflow.com/q/19842429/1893154) and make transparent the button you don't need by default, and enable and show it when you enter the screen – zeeMonkeez May 05 '16 at 23:36
  • The buttons will have different actions depending on the screen, I'm sorry - I should have included that detail in the question. I'm currently working on a possible solution that involves using ABC and inheritance, and just declaring all the buttons and lists inside the python code instead of using the Builder. I have now edited the question to add clarification. – jdanaher May 05 '16 at 23:54
  • That can be done using inheritance without problem, but that's too much writing for a single question. – jligeza May 06 '16 at 17:42
  • I've been struggling to make inheritance work, the syntax is a little wonky at least from what I've found. So I gave up for now and I'm working on a different screen for the app. – jdanaher May 06 '16 at 20:29

0 Answers0