'Delete a row in Kivy's BoxLayout

With Kivy, I want a layout that is different when the window size is portrait or landscape (a grid of buttons, and for exemple the buttons on a row are shown in a column).

I tried first to hide some widgets, but that doesn't work well (even at a size of 0, some text appears, and this doesn't work with spacing)

So I wanted to delete a row directly, it worked, but... The space for the raw remains with, like this Vertical mode Horizontal Mode

I tried to delete the second row, but there is now a gap.

Here is the code for that example test3.kv file

<MDBoxLayout>:
    spacing:5
        
<MyLayout>:
            
    MDBoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        id: vbox
        
        # Texte
        MDLabel:
            size_hint: 1,0.4
            text: "Text"
            halign: 'left'
            valign: 'bottom'
            font_size: 48
            id: label
         
        # Ligne 1   
        MDBoxLayout:
            orientation: "horizontal"
            size_hint: 1,0.15
            id: ligne1
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text: "A"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"B"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"C"

            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"D"
            
        # Ligne 2
        MDBoxLayout:
            orientation: "horizontal"
            size_hint: 1,0.15
            id: ligne2
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text: "1"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"2"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"3"
        
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"4"
            
        # Ligne 3
        MDBoxLayout:
            orientation: "horizontal"
            size_hint: 1,0.15
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text: "E"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"F"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"G"

            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"H"
            
        # Ligne 4
        MDBoxLayout:
            orientation: "horizontal"
            size_hint: 1,0.15
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text: "I"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"J"
            
            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"K"

            MDFillRoundFlatButton:
                size_hint: 0.25,1
                text:"L"                                

test3.py file

import kivy
kivy.require('1.1.1')

from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivymd.uix.boxlayout import MDBoxLayout

Builder.load_file('test3.kv')
           
class MyLayout(Widget):    
    def on_size(self, *args):
        self.ids.label.text = 'on_size'
        if self.width > self.height:
            self.ids.label.text = self.ids.label.text + f' horizontal {self.ids.ligne2}'
        # orientation : horizontal
            if isinstance(self.ids.ligne2, MDBoxLayout):
                self.ids.label.text = self.ids.label.text + ' ligne 2 existe: supprimée'
                self.ids.ligne2.clear_widgets()
                self.remove_widget(self.ids.ligne2)
                self.ids.ligne2 = None

class app(MDApp):
    def build(self):
        return MyLayout()

if __name__ == '__main__':
    app().run()

Is this something missing in my code, or should I try another method to change dynamically the layout?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source