Skip to content Skip to sidebar Skip to footer

How To Store To List In Kivy?

I am making kivy app, something like project manager. I've got problem, i'll try to describe it. In my app you can create new 'project' by pressing button, then open the project an

Solution 1:

Here is simple example of my problem.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
classTest(BoxLayout):
    x1 = 0.2
    top1 = 0.9
    widgets = []
    defadder(self, *args):
        lb1 = Label(text="HELLO WORLD", pos_hint={"x":self.x1,"top":self.top1})
        self.add_widget(lb1)
        self.x1 = self.x1 - 0.05
        self.top1 = self.top1 - 0.05
        self.widgets.append(lb1)
    defremover(self, *args):
        for i in self.widgets:
            self.remove_widget(i)
        self.x1=0.2
        self.top1 = 0.9def__init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)
        bt1 = Button(text="ADD", pos_hint={"x":.1,"top":1}, size_hint=(0.2,0.4))
        self.add_widget(bt1)
        removebt = Button(text = "REMOVE",pos_hint={"x":.4,"top":1}, size_hint=(0.2,0.4))
        self.add_widget(removebt)
        removebt.bind(on_release=self.remover)
        bt1.bind(on_release=self.adder)



classAPP(App):
    defbuild(self):
        return Test()
if __name__ == "__main__":
    APP().run()

If i want to delete only one label it will delete all. Just try to copy and run it

Solution 2:

Just remove the last Label in the list:

defremover(self, *args):
    iflen(self.widgets) > 0:
        self.remove_widget(self.widgets.pop(-1))

Solution 3:

OK i could delete last that's improvement, but my problem is like this. What if I with button "ADD" I add text and button to remove the text and the button itself. Here is my new example with yours improvment.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
Window.size = (400, 650)
classTest(BoxLayout):
    x1 = 0.1
    top1 = 0.9
    widgets = []
    defadder(self, *args):
        lb1 = Label(text="HELLO WORLD", pos_hint={"x":self.x1,"top":self.top1})
        self.add_widget(lb1)
        removebt = Button(text = "REMOVE",pos_hint={"x":self.x1-0.1,"top":self.top1}, size_hint=(0.5,0.1))
        self.add_widget(removebt)
        self.widgets.append(removebt)
        removebt.bind(on_release=self.remover)
        self.widgets.append(lb1)
        self.top1 = self.top1 - 0.05defremover(self, *args):
        iflen(self.widgets) > 0:
            self.remove_widget(self.widgets.pop(-1))
        self.x1=0.2
        self.top1 = 0.9def__init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)
        bt1 = Button(text="ADD", pos_hint={"x":.1,"top":1}, size_hint=(1,0.4))
        self.add_widget(bt1)
        bt1.bind(on_release=self.adder)



classAPP(App):
    defbuild(self):
        return Test()
if __name__ == "__main__":
    APP().run()

Post a Comment for "How To Store To List In Kivy?"