Riccardo Piana
Riccardo Piana

Reputation: 107

Kivy "KeyError: " (I have made a minimal reproducible example )

Note

This is not my code, I tried to create a minimal reproducible example.

Code

This is a part of my python code:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from PIL import Image
from kivy.clock import Clock
import threading


class MyFloatLayout(FloatLayout):
    def __init__(self, **kwargs):
        super(FloatLayout, self).__init__(**kwargs)

class GUI2App(App):
    remainingFolders=0
    totalFolders=0
    def update_progress(self):
        Clock.schedule_once(GUI2App.update_progress_after(self))
    def update_progress_after(self):
        print("running")
        print("Ids:\n",self.root.ids)
        loadingBar= self.root.ids['loadingBar']
        loadingBar.max=self.totalFolders
        loadingBar.value=self.remainingFolders

    def build(self):
        self.title = 'Past Paper Question Search 🔎'
        return MyFloatLayout()
    def initConcatenate(self):
        p = threading.Thread(target=self.concatenate)
        p.start()
    def concatenate(self):
        totfolders=1
        for x in range(50):
            totfolders+=1     
        self.totalFolders=int(totfolders)
        self.update_progress()

        for x in range(50):
            totfolders-=1
            self.remainingFolders=totfolders
            self.update_progress()

if __name__ == "__main__":
    GUI2App().run()

This is part of my kv file:

#:kivy 2.0.0
<MyFloatLayout>:
    ProgressBar:
        id: loadingBar
        max: 0
        value: 1
        pos_hint: {"x":0.2, "y":0.5}
        size_hint_x: 0.6
        size_hint_y: 0.1
    Button:
        text: "press me to change bar!"
        pos_hint: {"x":0.2, "y":0.6}
        size_hint_x: 0.6
        size_hint_y: 0.1
        on_press: app.initConcatenate()

Error

It throws me this error:

in update_progress
     loadingBar= self.ids['loadingBar']
 KeyError: 'loadingBar'

Extra Info

When I call the function it is called by another function in another class, to not get the error that one positional argument (self) is missing I call the function like this: "MyFloatLayout().update_progress()".

Idk if it helps but hopefully it does

Upvotes: 0

Views: 92

Answers (1)

John Anderson
John Anderson

Reputation: 38822

I cannot reproduce the error that you mention, but there are some other errors in your code. The main error is in your code:

def update_progress(self):
    Clock.schedule_once(GUI2App.update_progress_after(self))

This code is running GUI2App.update_progress_after(self) and trying to schedule the returned result of that method, but the return is None. To correct this error, replace the above code with:

def update_progress(self):
    Clock.schedule_once(self.update_progress_after)

and add an *args argument to the update_progress_after() method definition:

def update_progress_after(self, *args):

Also, if you use:

MyFloatLayout().update_progress()

You are creating a new instance of MyFloatLayout and calling the update_progress() method of that new instance. But that new instance of MyFloatLayout is not the one that is in your GUI, so calling its update_progress() is likely to have no effect.

Upvotes: 1

Related Questions