0

I need to create a screen as a history of your results form test, which was in another screens. I want use an MDExpansionPanel, but when I wrote a code nothing is shown. Screen is empty I don't know why, could anybody please help me?

In main.py

class GoalsScreen(Screen):
    pass
class Content(BoxLayout):
    pass

class HistoryScreen(Screen):

    def on_start(self):
        names = ["test1", "test2", "test3"]

        for name in names:
            panel= MDExpansionPanel(icon="1.png",title= name,
                    content=Content())

            self.root.ids.panel_container.add_widget(panel)

class MainApp(MDApp):

    def build(self):
        self.theme_cls.primary_palette = "Red"
        self.theme_cls.primary_hue = "500"
        self.theme_cls.theme_style = "Light"


        screen = Builder.load_string(screen_helper)
        return screen

MainApp().run()

In .kv


screen_helper = """
ScreenManager:
    GoalsScreen:
    HistoryScreen:

<GoalsScreen>:
    name: "goals"
    Button:
        text: "next page"
        on_press: root.manager.current= "history"
    
<Content>
    size_hint: 1,None
    height: self.minimum_height
    Button:
        size_hint: None, None
    MDIconButton:
        icon: "close"
    
<HistoryScreen>:
    name: "history"
    
    BoxLayout:
        ScrollView:
            GridLayout:
                cols: 1
                size_hint_y: None
                height: self.minimum_height
                id: panel_container

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

Several issues with your code:

  • The on_start() method is never called (it is not automatically called for a Screen).
  • The title= name is not a legal argument to MDExpansionPanel.
  • The required argument panel_cls to MDExpansionPanel is missing.
  • The HistoryScreen has no root attribute, so this code will fail: self.root.ids.panel_container.add_widget(panel)

Try something like this:

class HistoryScreen(Screen):
    def on_enter(self):
        self.ids.panel_container.clear_widgets()  # to avoid re-adding panel each time
        names = ["test1", "test2", "test3"]

        for name in names:
            panel= MDExpansionPanel(icon="1.png",panel_cls=MDExpansionPanelOneLine(),
                    content=Content())

            self.ids.panel_container.add_widget(panel)
John Anderson
  • 35,991
  • 4
  • 13
  • 36