Floatlayout not correctly adding to Gridlayout Kivy

Viewed 38

EDIT: So i tried to look at the problem with a canvas. What i discovered was that if i move the GridLayout on the x-axys the canvas moves like it should so i know that the GridLayout is not at the center anymore. The children of the GridLayout, the FloatLayouts, don't move tho. I am wondering how this can occur when i am adding the FloatLayouts directly to the GridLayout. Thank's for your help.

Here's what i mean in a screenshot. The canvas is lightblue and the Gridlayout is at pos_hint_x: .5 Example of Canvas and Children not moving

enter image description hereHey Guys So i have two for loops, one creating a float layout with an image and a name as children and one with a button and an image as children. The first one gets added to a grid layout with one column. This one works perfectly fine. The second one gets added to a grid layout with two columns and does a weird thing. It only shows every second item. So if i put 6 items in it only 3 show up. But if i print the Files from the for loop all 6 show up in the console. The odd thing is also that it always only shows them on the left side of the screen. You can see what i mean in the following screenshot:

In the following code, the for loop in the CreatePlan class is the one which is working fine and the for loop in the NextStep class is the one that is doing weird stuff.

Thank you!

.py:

class CreatePlan(Screen):
    name = "createplan"

    self_made_plans = {}
    ls_path = []
    spacing = 20
    row_height = 220
    widgetscreen = ObjectProperty(None)
    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.grd_details = None
        self.exercises_in_plan = []

        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)
        )

        self.scrl_view_2 = ScrollView(
            size_hint=(.7, .9),
            pos_hint={"x": 0, "y": 0},
            do_scroll_x=False,
            do_scroll_y=True
        )
        self.scrl_child_2 = 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))

        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=path: self.add_exercise_to_plan(x)
            )

            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):

        path = exercise.background_normal

        img = Image(
            source=path
        )

        self.widgetscreen.add_widget(img)
        self.exercises_in_plan.append(path)

    def test(self):
        print("plans before add: " + str(main.CreatePlan.plans))

        plan_name = self.ids.trainingplan_name.text

        if plan_name not in main.CreatePlan.plans:
            main.CreatePlan.self_made_plans[plan_name] = self.exercises_in_plan

        print("plans after add: " + str(main.CreatePlan.plans))

        self.exercises_in_plan = []


class NextStep(Screen):
    name = "nextstep"

    def __init__(self, **kwargs):
        super(NextStep, self).__init__()
        self.plan_name = MDApp.get_running_app().sm.get_screen("createplan").ids.trainingplan_name.text
        self.id_dict = {}
        self.plan_name = MDApp.get_running_app().sm.get_screen("createplan").ids.trainingplan_name.text

        self.grd_details = GridLayout(
            size_hint=(.8, .9),
            cols=3
            # pos_hint={"x": 0, "y": 0},
        )

        self.flt_name = FloatLayout(
            size_hint=(1, .1),
            pos_hint={"top": 1}
        )

        self.add_widget(self.flt_name)

    def update(self):
        lbl = Label(
            text=self.plan_name,
            color="black",
            size_hint=(1, 1),
            pos_hint={"center_x": 1, "y": 0}
        )

        self.flt_name.add_widget(lbl)

        create_plan_class = MDApp.get_running_app().sm.get_screen("createplan")
        set_details_class = MDApp.get_running_app().sm.get_screen("setdetails")

        for file in create_plan_class.exercises_in_plan:
            name_cleared = file.replace(main.all_exercises_path + "/", "")
            name_cleared = name_cleared.replace(".png", "")

            img = Image(
                source=file,
                size_hint=(1, .7),
                pos_hint={"y": .3}
            )

            btn = Button(
                text="Add Details",
                size_hint=(.5, .3),
                pos_hint={"y": 0},
                on_release=lambda x, pth=file: set_details_class.set_details_to_exercise(pth),
            )

            flt = FloatLayout(

            )

            flt.add_widget(img)
            flt.add_widget(btn)
            self.ids.grd.add_widget(flt)

            print(file)

.kv:

<CreatePlan>
widgetscreen: wdscreen

FloatLayout:
    size_hint: .7, .05
    pos_hint: {"x": 0, "y": .85}

    TextInput:
        id: trainingplan_name
        size_hint: .5, 1
        pos_hint: {"center_x": .55, "y": 0}
        multiline: False

    Label:
        text: "Name of the plan:"
        size_hint: .3, 1
        pos_hint: {"center_x": .15, "y": 0}
        color: "black"

ScrollView:
    size_hint: .7, .75
    pos_hint: {"x": 0, "y": .1}
    do_scroll_x: False
    do_scroll_y: True

    GridLayout:
        id: wdscreen
        cols: 2
        spacing: 30
        padding: 0, 20, 0, 20
        row_default_height: root.height*.7/3
        col_default_width: root.width*.65/2
        row_force_default: True
        col_force_default: True
        size_hint_x: None
        size_hint_y: None
        height: self.minimum_height

FloatLayout:
    size_hint: .7, .1
    pos_hint: {"x": 0, "y": 0}

    Button:
        text: "Create Plan"
        size_hint: .3, 1
        pos_hint: {"center_x": .5, "y": 0}
        on_release:
            app.root.current = "nextstep"
            app.root.get_screen("nextstep").update()

NavigationDrawer:

<NextStep>



 GridLayout:
        id: grd
    size_hint: 1, .9
    cols: 2

NavigationDrawer:
0 Answers
Related