pythonkivyremovechildboxlayout

How Can I Remove BoxLayout?


I want to remove a Boxlayout in my widget, but I cant.

There is a Boxlayout at the begining of the app called "Speca". After my sellections it must be removed.

When you run the app, sellect something in "Home" Spinner and click any of the new Toggle buttons.

Wtih the press of any of the Toggle buttons "Speca" must disappear.

Please help.

Here is main.py:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.spinner import Spinner
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
from kivy.properties import ListProperty
from collections import OrderedDict
from kivy.uix.togglebutton import ToggleButton

data1=["mother","father","son"]
data2=["uncle","aunt","grandfather"]
data3=["jack","mike","simon"]
data4=["1898","1975","1985","1885"]

amd="0dp"

class MainWidget(Widget):
    def remove_layout(self, *ignore):
        self.remove_widget(self.layout)

    global amd
    an0=tuple(list(OrderedDict.fromkeys(data1)))
    cal5= ObjectProperty()
    cal6= ObjectProperty()
    def btn10(self,text):

        if self.cal5:
            self.cal5.parent.remove_widget(self.cal5)

        self.cal5 =ModelSpecifications()


        a=data2
        b=data3
        c=data4

        mi=[]
        n=0

        while n < len(a):
            aba=n
            mi.append(aba)
            n+=1

        for i in mi:
            self.b1=MyTButton(text=str(i),size_hint=(1,None),height="100dp",group="selections")
            self.cal5.add_widget(self.b1)
        self.ids.scd.add_widget(self.cal5, index=3) 

    def on_state(self, togglebutton): #<----THIS IS THE TUGGLEBUTTON I WANT USE
        global amd
        tb = togglebutton
        text1=tb.text

        if text1=="0":
            amd="50dp"
        elif text1=="1":
            amd="100dp"
        else:
            amd="200dp"

        if self.cal6:
            self.cal6.parent.remove_widget(self.cal6)

        self.cal6 =Spec()
        self.ids.scd.add_widget(self.cal6, index=2)

class SecondPage(ScrollView):
    pass


class Speca(BoxLayout):  #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE
    pass


class Spec(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.size_hint=(1,None)
        self.height=amd


class MyTButton(ToggleButton):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        

class ModelSpecifications(BoxLayout): #this is the class I want add after my spinner selection 
    pass

class Calculation(GridLayout):
    pass   

class MyApp(App):
    pass

MyApp().run()

here is my.kv :

MainWidget:
<MainWidget>:
    hideable: hideable
    ScreenManager:
        id: scmanager
        size: root.width, root.height
        Screen:
            id: scndpage
            name: "second"
            SecondPage:
                Calculation:
                    id:scd            
                    cols:1 
                    height: self.minimum_height 
                    row_default_height: "70dp"
                    size_hint_y: None
                    spacing:"10dp"
                    canvas.before:
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    BoxLayout:

                        size_hint: 1, None
                        height: "50dp"
                        
                        pading:"10dp"
                        spacing:"10dp"
                        orientation: "vertical"
                        BoxLayout:
                            orientation: "horizontal"
                            Label:
                                text:"Name:"
                                color: 0,0,0,1
                            TextInput:
                                text:"---"
                                color: 0,0,0,1
                            Label:
                                text:"Surname:"
                                color: 0,0,0,1
                            TextInput:
                                text:"-----"
                                color: 0,0,0,1
                    BoxLayout:
                        id:scdd
                        size_hint: 1, 1
                        height: "100dp"
                        orientation: "vertical"
                        BoxLayout:
                            size_hint: 1, None
                            height: "50dp"
                            orientation: "horizontal"
                            Label:
                                text: " Sellection:"
                                color: 0,0,0,1
                            Spinner:
                                text: 'Home'
                                values: root.an0
                                on_text: app.root.btn10(self.text)   

                    Speca: #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE

                    Button:
                        text:" Calculate"

                    Button:
                        text:"Sellect"
                    
                    Button:
                        text:"Back"

    
<ModelSpecifications>:      
    id:anss                 
    pading:"10dp"
    spacing:"10dp"
    size_hint: 1, None
    height: "100dp"
    orientation: "horizontal"


<MyTButton@ToggleButton>: #<----THIS IS THE TUGGLEBUTTON I WANT USE
    on_state: 
        app.root.on_state(self)

<Speca>:   #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE
    orientation:"vertical"
    Label:  
        color: (1,1,0,1)
        text:"I WANT TO REMOVE THIS PART WHEN I PRESS ANY OF TOGGLE BUTTONS"

Please run the app and see it.


Solution

  • Looks to me like speca is a child of the calculation widget with ID scd. So give speca an ID and then Remove speca via ids in on_srate python function.

    Kv

    SecondPage:
        Calculation:
            id:scd
            speca:
                id: speca
    

    py

    def on_state():
        self.root.ids.scd.remove_widget(self.root.ids.speca)