Search code examples
pythonkivykivy-language

Kivy issue regarding buttons moving from center when resizing window


I am having a strange issue. The buttons in the bottom BoxLayout (submit and cancel) move from the center of my screen all the way to the left whenever i resize the window. However if i resize the window and then later go back to the screen via the screen manager, they are back in the center. Any advice or is there a better way to center them?

EDIT: here is a screenshot of my app on startup: https://i.sstatic.net/geMDu.png

AnchorLayout:
    size_hint_y: None
    height: max(root.height, content.height)
    GridLayout:
        id: content
        cols: 1
        spacing: '8dp'
        padding: '8dp'
        size_hint: (.8, None)
        height: self.minimum_height
        BoxLayout:
            size_hint_y: None
            height: '48dp'
            TextInput:
                id: textField
                multiline: False
        BoxLayout:
            size_hint_y: None
            height: '48dp'
            ProgressBar:
                id: downloadBar
                max: 1
                value: 0
        BoxLayout:
            size_hint: None, None
            height: '48dp'
            width: '150dp'
            center_x: root.center_x
            Button:
                text: "Submit"
                on_press:
                    TextScreen.download_file(textField.text)

            Button:
                text: "Cancel"
                on_press:
                    root.manager.transition.direction = 'left'
                    root.manager.transition.duration = 1
                    root.manager.current = 'Get_Vod'

Solution

  • the effect you describe is a bit unexpected to me, but the way you tried won't work well, since it'll not integrate well with the layouting code of GridLayout.

    You probably want to use pos_hint to center the BoxLayout, but this won't work directly in GridLayout, so you have the choice to either convert your GridLayout (with one col) into a vertical BoxLayout, or, if you can't do that for some reason, to wrap your current BoxLayout into another one, that would keep size_hint_x to 1, so it can properly center its child.

    switching to BoxLayout

    AnchorLayout:
        size_hint_y: None
        height: max(root.height, content.height)
        BoxLayout:
            id: content
            orientation: 'vertical'
            spacing: '8dp'
            padding: '8dp'
            size_hint: (.8, None)
            height: self.minimum_height
            BoxLayout:
                size_hint_y: None
                height: '48dp'
                TextInput:
                    id: textField
                    multiline: False
            BoxLayout:
                size_hint_y: None
                height: '48dp'
                ProgressBar:
                    id: downloadBar
                    max: 1
                    value: 0
            BoxLayout:
                size_hint: None, None
                height: '48dp'
                width: '150dp'
                pos_hint: {'center_x': .5}
                Button:
                    text: "Submit"
                    on_press:
                        TextScreen.download_file(textField.text)
    
                Button:
                    text: "Cancel"
                    on_press:
                        root.manager.transition.direction = 'left'
                        root.manager.transition.duration = 1
                        root.manager.current = 'Get_Vod'
    

    wrapping with a BoxLayout

    AnchorLayout:
        size_hint_y: None
        height: max(root.height, content.height)
        GridLayout:
            id: content
            cols: 1
            spacing: '8dp'
            padding: '8dp'
            size_hint: (.8, None)
            height: self.minimum_height
            BoxLayout:
                size_hint_y: None
                height: '48dp'
                TextInput:
                    id: textField
                    multiline: False
            BoxLayout:
                size_hint_y: None
                height: '48dp'
                ProgressBar:
                    id: downloadBar
                    max: 1
                    value: 0
            BoxLayout:
                size_hint_y: None
                height: '48dp'
                BoxLayout:
                    size_hint_x: None
                    width: '150dp'
                    pos_hint: {'center_x': .5}
                    Button:
                        text: "Submit"
                        on_press:
                            TextScreen.download_file(textField.text)
    
                    Button:
                        text: "Cancel"
                        on_press:
                            root.manager.transition.direction = 'left'
                            root.manager.transition.duration = 1
                            root.manager.current = 'Get_Vod'