-1

I know in python class attributes are shared across instances of a class. I'm trying to write a kivy app in which the first screen has an add button which can be pressed to add more buttons. The buttons are kept track of in a list, and when a new button is created it is added to the list.

When a button is pressed the screen switches to another instance of the class that defines the first screen. As it stands, this screen is populated with the list of buttons from the previous screen. Is there a way to have a different list for each instance of the class?

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.properties import NumericProperty, ReferenceListProperty,\
    ObjectProperty
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen

class TasksWindow(Screen):
    btnHeight=.9
    btnList=[]
    def __init__(self):
        super(TasksWindow, self).__init__()
        addBtn = Button(text = "+",pos_hint= {'x': .9,'top': 1},size_hint = (.1,.1))
        self.add_widget(addBtn)
        addBtn.bind(on_press=self.clkAdd)
        backBtn = Button(text = "Go Back",pos_hint= {'x': 0,'top': 1},size_hint = (.1,.1))
        backBtn.bind(on_press=self.clkBack)
        self.add_widget(backBtn)

    def clkAdd(self, obj):
        task = Task()

    def update(self):
        for x in range (0, len(self.btnList)):
            temp = self.btnList.pop(x)
            if (x == 0):
                TasksWindow.btnHeight = .9
            temp2 = Task()
            self.btnList.insert(x,temp2)
            del(self.btnList[len(self.btnList)-1])
            self.add_widget(temp2.editBtn)

    def preupdate(self,dt):
        bPoint = -1
        for x in range (0, len(self.btnList)):
            self.remove_widget(self.btnList[x])
        self.update()

    def clkBack(self, obj):
        sm.current = 'Screen 0'

class Task(TasksWindow):
    def __init__(self):
        if len(TasksWindow.btnList)==0:
            TasksWindow.btnHeight = .9
        self.editBtn = Button(text = "Tap to Edit",pos_hint= {'x': 0,'top': TasksWindow.btnHeight},size_hint = (.9,.1))
        self.editBtn.bind(on_press=self.clkEdit)
        TasksWindow.btnList.append(self)
        TasksWindow.btnHeight -= .1

    def clkEdit(self, obj):
        screen = TasksWindow()
        screen.name = ('Screen 1')
        sm.add_widget(screen)
        sm.current = ('Screen 1')
        Clock.schedule_interval(screen.preupdate, 1.0/2.0)

sm = ScreenManager()
screen = TasksWindow()
screen.name = 'Screen 0'
sm.add_widget(screen)
sm.current = 'Screen 0'
Clock.schedule_interval(screen.preupdate, 1.0/2.0)

class TasksApp(App):
    def build(self):
        return sm

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

It's not shown here, but the reason I use preupdate() and update() is so I can delete buttons. I clear all buttons, delete the deleted button from the list, and then call update().

1 Answers1

0

Is there a way to have a different list for each instance of the class?

Sure, just make the list an instance attribute by putting something like self.the_list = [] in your __init__.

inclement
  • 29,124
  • 4
  • 48
  • 60