FaultyProgrammer3107
FaultyProgrammer3107

Reputation: 221

Not able to Dynamical Update Kivy Label Text

I am trying to add rows to a table like GUI. These rows are to be a list of labels. Each row that is updated has the following methods in that class:

I then have initialized a list of objects ContentList[] for this class and call the methods But no matter which object's ChangeLabelText is called, only the text for ContentList[0] is updated.

import json
import requests
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.app import runTouchApp

class AddContent(GridLayout):
    #response = requests.get("http://localhost:10010/")
    # Get the response data as a python object.  Verify that it's a dictionary.
    #data = response.json()[3]
    #Column_keys = ["country", "date", "answered_calls", "total_calls", "asr", "mou", "aou"]
    RowList = []
    Label_List = []
    size = 0
    def AddRow(self, InputList):
        self.RowList = InputList
        self.size = len(InputList)

    def AddLabel(self,LayoutObj):
        lbl = Label(size_hint_y = None, height = 30)
        LayoutObj.add_widget(lbl)
        return lbl

    def AddLabelRow(self,LayoutObj):
        for i in range(self.size):
            Lbl = self.AddLabel(LayoutObj)
            Lbl.text = "empty"
            #self.Label_List[i].text = data[Column_keys[i]]
            #Lbl.text = str(self.data[self.Column_keys[i]])
            self.Label_List.append(Lbl)

    def ChangeLabel_ListText(self, TextList):
        for i in range(self.size):
            #self.Label_List[i].text = data[Column_keys[i]] #data is fetched from Db
            self.Label_List[i].text  = TextList[i]



class TableView(GridLayout):
    Col_Names = ["Date","Vendor","Country","MOU","ASR","AOU"]
    ContentList = [AddContent(),AddContent(),AddContent()]
    def __init__(self,**kwargs):
        self.layout  = GridLayout(cols = len(self.Col_Names), padding =5)
        self.layout.bind(minimum_height=self.layout.setter('height'))
        for i in range(len(self.Col_Names)):
            btn = Button(text=self.Col_Names[i], size_hint_y=None, height=30)
            self.layout.add_widget(btn)

        self.ContentList[0].AddRow(['1sample1','1sample2','1sample3','1sample4','1sample5','1sample6'])
        self.ContentList[1].AddRow(['2sample1','2sample2','2sample3','2sample4','2sample5','2sample6'])
        self.ContentList[2].AddRow(['3sample1','3sample2','3sample3','3sample4','3sample5','3sample6'])

        for i in range(3):
            self.ContentList[i].AddLabelRow(self.layout)

        self.ContentList[2].ChangeLabel_ListText(['a','b','c','d','e','f'])

if __name__ == '__main__':
    Table = TableView()
    runTouchApp(Table.layout)

The line self.ContentList[2].ChangeLabel_ListText(['a','b','c','d','e','f']) updates only the first row whatever number is given for the index.

I have been breaking my head over this for the past week. I had initially done this with only one class instead of two which gave the same output. Any help will help greatly. Thanks!

Upvotes: 0

Views: 68

Answers (1)

yogabonito
yogabonito

Reputation: 657

Your first problem is that RowList, Label_List, and size are class attributes in your code. But you want to set them in every instance individually. Solution: initialize these attributes inside the __init__ method like this:

def __init__(self, **kwargs):
    super(AddContent, self).__init__(**kwargs)
    self.RowList = []
    self.Label_List = []
    self.size = 0 

The second problem is, that the GridLayout (which your class is subclassing) also contains an attribute called size. Solution: pick a different name for this attribute like so:

    self.length = 0

If you now do

self.ContentList[i].ChangeLabel_ListText(['a','b','c','d','e','f'])

your i-th ContentList-entry will be changed.

Here is the complete solution:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.app import runTouchApp

class AddContent(GridLayout):
    #response = requests.get("http://localhost:10010/")
    # Get the response data as a python object.  Verify that it's a dictionary.
    #data = response.json()[3]
    #Column_keys = ["country", "date", "answered_calls", "total_calls", "asr", "mou", "aou"]
    def __init__(self, **kwargs):
        super(AddContent, self).__init__(**kwargs)
        self.RowList = []
        self.Label_List = []
        self.length = 0

    def AddRow(self, InputList):
        self.RowList = InputList
        self.length = len(InputList)

    def AddLabel(self,LayoutObj):
        lbl = Label(size_hint_y=None, height=30)
        LayoutObj.add_widget(lbl)
        return lbl

    def AddLabelRow(self,LayoutObj):
        for i in range(self.length):
            Lbl = self.AddLabel(LayoutObj)
            Lbl.text = "empty"
            #self.Label_List[i].text = data[Column_keys[i]]
            #Lbl.text = str(self.data[self.Column_keys[i]])
            self.Label_List.append(Lbl)

    def ChangeLabel_ListText(self, TextList):
        for i in range(self.length):
            #self.Label_List[i].text = data[Column_keys[i]] #data is fetched from Db
            self.Label_List[i].text  = TextList[i]



class TableView(GridLayout):
    Col_Names = ["Date","Vendor","Country","MOU","ASR","AOU"]
    ContentList = [AddContent(),AddContent(),AddContent()]
    def __init__(self,**kwargs):
        self.layout  = GridLayout(cols = len(self.Col_Names), padding=5)
        self.layout.bind(minimum_height=self.layout.setter('height'))
        for i in range(len(self.Col_Names)):
            btn = Button(text=self.Col_Names[i], size_hint_y=None, height=30)
            self.layout.add_widget(btn)

        self.ContentList[0].AddRow(['1sample1','1sample2','1sample3','1sample4','1sample5','1sample6'])
        self.ContentList[1].AddRow(['2sample1','2sample2','2sample3','2sample4','2sample5','2sample6'])
        self.ContentList[2].AddRow(['3sample1','3sample2','3sample3','3sample4','3sample5','3sample6'])

        for i in range(3):
            self.ContentList[i].AddLabelRow(self.layout)

        self.ContentList[2].ChangeLabel_ListText(['a','b','c','d','e','f'])

if __name__ == '__main__':
    Table = TableView()
    runTouchApp(Table.layout)

Upvotes: 1

Related Questions