Search code examples
androidpythonpython-3.xkivykivy-language

how do I use the remove_widget() function?


I have this code.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty


class Temp(BoxLayout):
    def __init__(self, iid):
        BoxLayout.__init__(self)
        self.id = str(iid)

    def remove_content(self, iid):
        #How me need use remove_widget() function?
        #self.ids['tmp'].remove_widget(?)

class ScreenApp(BoxLayout):
    def __init__(self):
        BoxLayout.__init__(self)
        self.idsum = 0
        self.idsum+=1
        self.ids['contents'].add_widget(Temp(iid = self.idsum))
        self.idsum+=1
        self.ids['contents'].add_widget(Temp(iid = self.idsum))

    def add_content(self):
        self.idsum+=1
        self.ids['contents'].add_widget(Temp(iid = self.idsum))

class MainApp(App):
    def build(self):
        return ScreenApp()

if __name__ == "__main__":
    MainApp().run()

and this

<Temp@BoxLayout>
    id: tmp
    Button:
        id: idti
        text: "-"
        on_press: root.remove_content(id)
    Label:
        id: idlbl
        text: "Temp"

<ScreenApp>:
    BoxLayout:
        orientation: "vertical"

        BoxLayout:
            id: contents
            orientation: "vertical"

        Button:
            text: "+"
            on_press: root.add_content()

I want to remove widgets at the click of a button and to the bottom widgets rose to the place of the remote widget TM most without leaving an empty space. Thank you in advance. a few more words to remove the pop-up.


Solution

  • Use self.ids.contents.remove_widget(instance) to remove an instance of object. Please refer to the explanations and example for details.

    kv file

    1. Replace dynamic class, <Temp@BoxLayout> with class rule, <Temp>:
    2. Remove id: tmp because it is not needed
    3. Replace root.remove_content(id) with app.root.remove_content(root) because we are moving the method into class ScreenApp() since we already have add_content() method there.
    4. Instead of passing id as argument, we are passing root i.e. an instance of Temp object to be removed.
    5. Replace text: 'Temp' with root.text for visualisation.
    6. Removed the first BoxLayout: under class rule, <ScreenApp>: because it is already a BoxLayout and don't really need another BoxLayout i.e.nested BoxLayouts.

    Python code

    1. Added text as argument to constructor method of class Temp().
    2. Added self.text = text for assignment
    3. Added text='Temp' + str(self.idsum) when instantiating Temp object

    Example

    main.py

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    
    
    class Temp(BoxLayout):
        def __init__(self, iid, text, **kwargs):
            super(Temp, self).__init__(**kwargs)
            self.id = str(iid)
            self.text = text
    
    
    class ScreenApp(BoxLayout):
        def __init__(self):
            BoxLayout.__init__(self)
            self.idsum = 0
            self.idsum += 1
            self.ids['contents'].add_widget(Temp(iid=self.idsum, text='Temp' + str(self.idsum)))
            self.idsum += 1
            self.ids['contents'].add_widget(Temp(iid=self.idsum, text='Temp' + str(self.idsum)))
    
        def add_content(self):
            self.idsum+=1
            self.ids['contents'].add_widget(Temp(iid=self.idsum, text='Temp' + str(self.idsum)))
    
        def remove_content(self, instance):
            self.ids.contents.remove_widget(instance)
    
    
    class MainApp(App):
        def build(self):
            return ScreenApp()
    
    
    if __name__ == "__main__":
        MainApp().run()
    

    main.kv

    #:kivy 1.11.0
    
    <Temp>:
        text: 'Temp'
        Button:
            id: idti
            text: "-"
            on_press: app.root.remove_content(root)
        Label:
            id: idlbl
            text: root.text
    
    <ScreenApp>:
        orientation: "vertical"
    
        BoxLayout:
            id: contents
            orientation: "vertical"
    
        Button:
            text: "+"
            on_press: root.add_content()
    

    Output

    Img01 - 6 Temp object added Img02 - Removed Temp3