python - Can't bind a custom event to the on_resize function in kivy - Stack Overflow

I am trying to build an app where on one screen within a portion of it there will be a buttons that is

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

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

Share Improve this question asked Mar 7 at 10:08 user22436820user22436820 54 bronze badges 1
  • Could you also provide the system errors that come up? – Uber Commented Mar 7 at 10:09
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744937097a4602092.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信