So i am getting the ` KeyError: 'gridlayoutexercises' when trying to call a function with a Button. I am referencing the layout through an id which throws the Error. I tried other solutions from the web but they didn't work for me. I also adde @mainthread which it now doesn't throw the error immediately when i start the application. The error still occurs when i press the button.
.py:
class CreatePlan(Screen):
name = "createplan"
spacing = 20
row_height = 220
scr_minimum_height = len(os.listdir(main.all_exercises_path)) * row_height + (len(os.listdir(main.all_exercises_path)) * spacing)
def __init__(self, **kwargs):
super(CreatePlan, self).__init__()
self.scrl_view = ScrollView(
size_hint=(.3, .9),
pos_hint={"x": .7, "y": 0},
do_scroll_x=False,
do_scroll_y=True
)
self.scrl_child = GridLayout(
cols=1,
size_hint_x=1,
size_hint_y=None,
height=self.scr_minimum_height,
row_default_height=self.row_height,
row_force_default=True,
spacing=self.spacing,
padding=(10, 15, 10, 10)
)
srtd = sorted(os.listdir(main.all_exercises_path))
print("Sorted list: " + str(srtd))
for file in srtd:
path = main.all_exercises_path + "/" + file
exercises_names = file.split(".")[0].replace("_", " ")
btn = Button(
background_normal=path,
size_hint_y=.9,
pos_hint={"x": 0, "y": 0},
on_release=lambda x:self.add_exercise_to_plan(path)
)
lbl = Label(
text=exercises_names,
pos_hint={"x": 0, "y": .47}
)
flt = FloatLayout(
)
flt.add_widget(btn)
flt.add_widget(lbl)
self.scrl_child.add_widget(flt)
self.add_widget(self.scrl_view)
self.scrl_view.add_widget(self.scrl_child)
@mainthread
def add_exercise_to_plan(self, exercise):
img = Image(
source=exercise,
)
self.ids.gridlayoutexercises.add_widget(img)
.kv:
<CreatePlan>
canvas:
Color:
rgb: 0, 0, 0
Rectangle:
size: (self.width*.3, self.height)
pos: (self.width*.7, 0)
GridLayout:
id: "gridlayoutexercises"
cols: 2
size_hint: .7, 1
canvas:
Color:
rgb: 0, 0, .7
Rectangle:
size: self.size
NavigationDrawer:
The full Error looks like this:
Traceback (most recent call last):
File "kivy/properties.pyx", line 961, in kivy.properties.ObservableDict.__getattr__
KeyError: 'gridlayoutexercises'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/main.py", line 175, in <module>
main_app.run()
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/venv/lib/python3.9/site-packages/kivy/app.py", line 955, in run
runTouchApp()
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/venv/lib/python3.9/site-packages/kivy/base.py", line 574, in runTouchApp
EventLoop.mainloop()
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/venv/lib/python3.9/site-packages/kivy/base.py", line 339, in mainloop
self.idle()
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/venv/lib/python3.9/site-packages/kivy/base.py", line 379, in idle
Clock.tick()
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/venv/lib/python3.9/site-packages/kivy/clock.py", line 733, in tick
self.post_idle(ts, self.idle())
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/venv/lib/python3.9/site-packages/kivy/clock.py", line 776, in post_idle
self._process_events()
File "kivy/_clock.pyx", line 620, in kivy._clock.CyClockBase._process_events
File "kivy/_clock.pyx", line 653, in kivy._clock.CyClockBase._process_events
File "kivy/_clock.pyx", line 649, in kivy._clock.CyClockBase._process_events
File "kivy/_clock.pyx", line 218, in kivy._clock.ClockEvent.tick
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/venv/lib/python3.9/site-packages/kivy/clock.py", line 1095, in callback_func
func(*args, **kwargs)
File "/Users/physiodreieichen/Desktop/Pycharm_projects/TrainingApp/makeplans.py", line 82, in add_exercise_to_plan
self.ids.gridlayoutexercises.add_widget(img)
File "kivy/properties.pyx", line 964, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
Thanks for your help!