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

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página