Luigi Matera
Luigi Matera

Reputation: 23

Kivy add_widget does not show new widgets

I'm trying to add widgets in a Scrollview placed inside a Screen, but I can not understand what is the problem with the code. The code does not give me any error but it does not show the new widget. When the button "add" is pressed, the funcion add() is called but it does not work.

Here there is the code.py

class ScreenManagement(ScreenManager):
    pass    
class Screen1(Screen):
    pass    
class Screen2(Screen):
    pass    
class ClassAllScreen(BoxLayout):
    pass        
class ClassApp(App):    
    def add(self):   
        self.s2 = Screen2()    
        layout = GridLayout(cols=3)
        layout.add_widget(Label(text='Test'))
        layout.add_widget(TextInput())
        layout.add_widget(CheckBox())     
        self.s2.ids.widget_list.add_widget(layout)
    def build(self):
        self.root = ClassAllScreen()
        return self.root    
kv = Builder.load_file("main2.kv")    
if __name__ == '__main__':
    ClassApp().run()

and the kv file:

<Screen1>:    
    GridLayout:
        cols:1        
<Screen2>:    
    GridLayout:    
        cols:1    
        Button:
            size_hint_y: 0.1
            text: 'add'
            on_release:
                app.add()    
        ScrollView:    
            size_hint: 1, 1
            do_scroll_x: False
            padding: 20
            spacing: 20    
            GridLayout:   
                id: widget_list
                cols: 1
                spacing: 5    
                GridLayout:
                    rows: 1        
<ScreenManagement>:    
    Screen1:
        name: 'screen1'
    Screen2:
        name: 'screen2'    
<ClassAllScreen>:    
    orientation:'vertical'    
    GridLayout:
        cols: 1    
        GridLayout:    
            rows: 1
            size_hint_y: 0.12    
            Button:    
                text: "Screen 2"
                    on_release:
                    app.root.ids.screenmanager.transition.direction = 'right'
                    app.root.ids.screenmanager.current='screen2'    
            Button:    
                text: "Screen 1"    
                on_release:
                    app.root.ids.screenmanager.transition.direction = 'left'
                    app.root.ids.screenmanager.current='screen1'    
        ScreenManagement:
            id:screenmanager

Can someone help me?

Upvotes: 1

Views: 98

Answers (1)

John Anderson
John Anderson

Reputation: 38822

In your add() method, the code:

self.s2 = Screen2()

is creating a new instance of Screen2. Then you are adding widgets to that new instance. However, that new instance is not part of your GUI. What you want is to add the widgets to the Screen2 instance that is in your GUI. To do that, replace the above code with:

self.s2 = self.root.ids.screenmanager.get_screen('screen2')

This code gets the ScreenManager using its id, and then uses get_screen() to the the Screen2 instance.

Upvotes: 1

Related Questions