Changing layout form on button press Kivy

Viewed 56

I am new to kivy and currently trying to make an application. I have this code I am writing, I need to start with boxlayout because of the dropdown menu, but after that I wanna change to Gridlayout on the press of the startbutton. If anyone could give me a soulution I would be really thankful.

    Builder.load_string("""
    
    <CustomDropDown>
        Button:
            text: "Nobara"
            size_hint_y: None
            height: 44
            on_release: root.select("Nobara")
        Button:
            text: "Maki"
            size_hint_y: None
            height: 44
            on_release: root.select("Maki")
            
    """)
    
    class CustomDropDown(DropDown):
        pass
    
    xy = ""

   class MainApp(App):
        def build(self):
            self.startbutton = Button(text='Start',
                            size_hint=(.2, .2),
                            pos_hint={'center_x': .5, 'center_y': .5})
            self.startbutton.bind(on_press=self.on_press_startbutton)
    
            self.dropbutton = Button(text='Karakter',
                            size_hint=(.2, .2),
                            pos_hint={'center_x': .50, 'center_y': .5})
    
            dropdown = CustomDropDown()
            self.dropbutton.bind(on_release=dropdown.open)
            dropdown.bind(on_select = lambda instance, x : setattr(self.dropbutton, 'text', x))
            dropdown.bind(on_select = lambda instance, x : karaktervalasztas(x))
    
            self.visszabutton = Button(text="Kilépés",
                            size_hint=(.2, .2),
                            pos_hint={'center_x': .5, 'center_y': .5})
            self.visszabutton.bind(on_press=self.on_press_visszabutton)
    
            """self.huzasbutton = Button(text=f"Huzas: {len(pakli)}",
                            size_hint=(.2, .2),
                            pos_hint={'center_x': .5, 'center_y': .5})
            self.huzasbutton.bind(on_press=self.on_press_kartyahuzas)"""
    
    
            boxlayout = BoxLayout()
            boxlayout.add_widget(self.startbutton)
            boxlayout.add_widget(self.dropbutton)
            return boxlayout
    
        def on_press_startbutton(self, instance):
            boxlayout = GridLayout(cols=10)
            self.root.clear_widgets()
            self.root.add_widget(self.visszabutton)
            self.root.add_widget(Button(text=f"Huzas: {len(pakli)}", size_hint=(.2, .2), pos_hint={'center_x': .5, 'center_y': .5}, on_press=self.on_press_kartyahuzas))
            self.root.add_widget(Button(text=f"Dobópakli: {len(dobopakli)}", size_hint=(.2, .2), pos_hint={'center_x': .5, 'center_y': .5}, on_press=self.on_press_dobopakli))
            start()
            for i in hand:
                #self.root.add_widget(Button(text=str(i), on_press=lambda *args: self.on_press_kartya()))
                self.root.add_widget(Button(size_hint=(.2, .2), background_normal=i+ '.jpg', on_press=self.on_press_kartya))
            return boxlayout
1 Answers

Why not just use GridLayout from the start? In you build() method:

boxlayout = GridLayout(cols=2, size_hint_y=0.25)

Then, in your on_press_startbutton():

    def on_press_startbutton(self, instance):
        # boxlayout = GridLayout(cols=10)
        self.root.clear_widgets()
        self.root.cols = 10
        self.root.size_hint_y = 1.0
        self.root.add_widget(self.visszabutton)
        .
        .
        .

and the return boxlayout can be removed from the on_press_startbutton() method.

Related