Search code examples
pythonkivykivy-language

How to update RecycleView Kivy


This is my first question, so sorry if butcher it.

I'm making an application, that will allow for a more efficient protocol creation for other people at my job.

The problem is, that I'm trying to update created RecycleView, but for some reason it doesn't work. Some solutions on the web advise using .refresh_from_data() method, but it did not work, and all the other solutions are too complicated (or I'm too dumb).

I have this function - add_entry that adds information from two TextInputs as a dictionary inside protocol list.

At the current moment my RecycleView just shows numbers, because no solution have worked, and I actually really struggled to even make a Recycle View.

Here is relevant part of Python code:

class DrillingInfoPage(Screen):
    rod = 1
    dist = 3
    protocol = ListProperty() # {Rod:_,Distance:_,Proc:_,Depth:_}

    def add_entry(self, proc, depth): 
        self.protocol.append({'Rod': 0, 'Distance': 0, 'Proc': 0, 'Depth': 0})
        self.protocol[self.rod-1]['Proc'] = proc
        self.protocol[self.rod-1]['Depth'] = depth
        self.protocol[self.rod-1]['Rod'] = self.rod
        self.protocol[self.rod-1]['Distance'] = self.dist
        self.rod += 1
        self.dist += 3
        print(self.protocol)
        return self.protocol


class Profile(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class ColorsPopup(Screen):
    popupWindow = None


class Recycle(RecycleView):
    def __init__(self, **kwargs):
        super(Recycle, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(50)]


kv = Builder.load_file("my.kv")


class MyApp(App):
    def build(self):
        return kv


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

And here is a relevant part of KV file:

<DrillingInfoPage>:
    name: 'third'
    BoxLayout:
        orientation: 'vertical'
        Label:
            size_hint: 1, .4
            text: 'Drilling Info Page'
        GridLayout:
            size_hint: 1, .1
            cols:3
            GridLayout:
                cols:2
                Label:
                    text: 'BG'
                TextInput:
                    id: start
                    multiline: False
            GridLayout:
                cols:2
                Label:
                    text: 'BG'
                TextInput:
                    id: end
                    multiline: False
            Button:
                text: 'Confirm'
                on_release: drilling_holes.text = 'BG' + start.text + ' -----> ' + 'BG' + end.text

        GridLayout:
            size_hint: 1, .1
            cols:3
            GridLayout:
                cols:2
                Label:
                    text: '%:'
                TextInput:
                    id: proc
                    multiline: False
            GridLayout:
                cols:2
                Label:
                    text: 'Depth:'
                TextInput:
                    id: depth
                    multiline: False
            Button:
                text: 'Add'
                on_release: root.add_entry(proc.text, depth.text)

        Label:
            id: drilling_holes
            size_hint: 1, .2
            text: ''

        Recycle:
            id: drilling_data
            data: self.data
            viewclass: 'Label'
            RecycleBoxLayout:
                default_size: None, '25dp'
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'

        Label:
            size_hint: 1, .2
            text: ''
        GridLayout:
            size_hint: 1, .17
            cols:2
            Button:
                text: 'Go Back'
                on_release:
                    app.root.current = 'second'
                    root.manager.transition.direction = 'down'
            Button:
                text: 'Confirm'
                on_release:
                    app.root.current = 'last'
                    root.manager.transition.direction = 'up'

I've tried creating some functions inside RecycleView class that would refresh the data, as well as inside the DrillingInfoPage class, but nothing seems to work.

I'm new to Python and especially Kivy, therefore hoping someone wise can guide me in the right direction :)

Here how the screen itself looks right now, ideally it should be empty at first and pressing 'Add' button should add a new line


Solution

  • You just have to add the new information to the data list of the RecycleView. It's not clear exactly what you want to add to the RecycleView, but you can just add a line to your add_entry() method like this:

        self.ids.drilling_data.data.append({'text': proc})  # add new entry to data list
    

    And that method does not need a return statement