pythonpython-3.xkivy

Can't bind a custom event to the on_resize function in kivy


I am trying to build an app where on one screen within a portion of it there will be a buttons that is dependent on how many "folders" the user creates, I want there to be a dynamic number of cols in the gridLayout that will hold these buttons and the number of cols should change based on the size of the window

My kv file for the app contains:

            ScrollView: 
                do_scroll_x: False
                do_scroll_y: True
                scroll_distance: "50dp"
                FolderGridLayout:
                    spacing: 20  # Add spacing between the buttons
                    padding: [20, 0, 20, 20]  # Add padding around the GridLayout
                    id: folderGrid
                    cols: 5
                    size_hint: (1, None)
                    height: self.minimum_height
                    row_default_height: "100dp"
                    row_force_default: True
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"
                    Button:
                        text: "Placeholder"
                        size_hint_x: None
                        width: "200dp"

and in my py file I have:

class FolderGridLayout(GridLayout):
    def __init__(self, **kwargs):
        super(FolderGridLayout, self).__init__(**kwargs)
        Window.bind(on_resize=self.adjust_cols)
    

    def adjust_cols(self, instance, window_width, window_height):
        gridLayout = self.ids["folderGrid"]
        print(gridLayout, "window_width, window_height")
        btnWidth = dp(200)
        spacing = dp(20)
        padding = dp(20)
        availableSpace = window_width - 2 * padding
        cols = max(1, int(availableSpace / (btnWidth + spacing)))  # Ensure there is at least one column
        gridLayout.cols = cols

But every time I resize the window I get a bunch of system errors from kivy I can't seem to find much online about how to solve this I've looked at the docs and googled a bunch


Solution

  • Your line of code:

    gridLayout = self.ids["folderGrid"]
    

    is trying to access an id that actually points to itself. Just replace that line with:

    gridLayout = self
    

    Note that the ids defined in a kv file will appear in the ids dictionary of the root object of the rule that contains the id definition. So those ids will not appear in the FolderGridLayout object. The ids will appear in the ScrollView (based on the limited code you have provided). See the documentation.