Kivy - base application has strange alignment

后端 未结 3 888
一生所求
一生所求 2020-12-20 19:29

I am trying to build a basic Kivy app. After adding the basic elements, and running the app, all of the elements are crammed into the bottom left corner. It shows up like th

相关标签:
3条回答
  • 2020-12-20 20:15

    I recently wrote a post of a little trick I use when I am programming interfaces. The trick will let you see a border around all the widgets (layouts included) you add to the screen. This would be the result for your code:

    enter image description here

    It takes advantage of inheritance and the Kivy rules to overwrite the base class of all widgets. You just have to add:

    <Widget>:
        canvas.after:
            Line:
                rectangle: self.x+1,self.y+1,self.width-1,self.height-1
    

    at the beginning of this file:

    <SublimeLauncher>:
        FloatLayout:
            BoxLayout:
                orientation: 'vertical'
                spacing: 10
                Label:
                    text: "Enter the path to the folder to open.\nPress OK if you would like to open without a directory"
                TextInput:
                    id: folderpath
                Button:
                    text: 'OK'
    
    0 讨论(0)
  • 2020-12-20 20:27

    Your layout has a default size of 100x100 pixels. You can try to color it to see how much space does it take:

    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.lang import Builder
    
    kv = '''
    <SublimeLauncher>:
        BoxLayout:
            canvas:
                Color:
                    rgb: 1, 0, 0
                Rectangle:
                    size: self.size
            orientation: 'vertical'
            spacing: 10
            Label:
                text: "Enter the path to the folder to open.\\nPress OK if you would like to open without a directory"
            TextInput:
                id: folderpath
            Button:
                text: 'OK'
    '''
    Builder.load_string(kv)
    
    class SublimeLauncher(Widget):
        pass
    
    class SublimeLauncherApp(App):
        def build(self):
            return SublimeLauncher()
    
    if __name__ == "__main__":
        SublimeLauncherApp().run()
    

    Setting non-default size:

    kv = '''
    <SublimeLauncher>:
        BoxLayout:
            size: 250, 250
            canvas:
                Color:
                    rgb: 1, 0, 0
                Rectangle:
                    size: self.size
            orientation: 'vertical'
            spacing: 10
            Label:
                text: "Enter the path to the folder to open.\\nPress OK if you would like to open without a directory"
            TextInput:
                id: folderpath
            Button:
                text: 'OK'
    '''
    Builder.load_string(kv)
    

    Taking full space:

    kv = '''
    <SublimeLauncher>:
        BoxLayout:
            size: root.size
            canvas:
                Color:
                    rgb: 1, 0, 0
                Rectangle:
                    size: self.size
            orientation: 'vertical'
            spacing: 10
            Label:
                text: "Enter the path to the folder to open. \\nPress OK if you would like to open without a directory"
            TextInput:
                id: folderpath
            Button:
                text: 'OK'
    '''
    Builder.load_string(kv)
    
    0 讨论(0)
  • 2020-12-20 20:29

    As your root widget is not a layout (you made SublimeLauncher inherit Widget), it doesn't set its children size/positions. So your FloatLayout have the defaults, since you don't override them manually either.

    pos: 0, 0
    size: 100, 100
    

    And these defaults of course constraints the child, since FloatLayout by constraint their size based on their size_hint property.

    You want to give them more space, as Nykakin pointed out.

    Also, as your text is bigger than the Label (you didn't set halign and text_size either) its texture is centered on the center of the Label, and so some part of it is out of screen. You want to have a look at kivy/examples/widgets/textalign.py

    0 讨论(0)
提交回复
热议问题