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.
Use self.ids.contents.remove_widget(instance)
to remove an instance of object. Please refer to the explanations and example for details.
<Temp@BoxLayout>
with class rule, <Temp>:
id: tmp
because it is not neededroot.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.id
as argument, we are passing root
i.e. an instance of Temp
object to be removed.text: 'Temp'
with root.text
for visualisation.BoxLayout:
under class rule, <ScreenApp>:
because it is already a BoxLayout and don't really need another BoxLayout i.e.nested BoxLayouts.text
as argument to constructor method of class Temp()
.self.text = text
for assignmenttext='Temp' + str(self.idsum)
when instantiating Temp
objectfrom 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()
#: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()