Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

python - Kivy - base application has strange alignment

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 this on android and Linux.

Main.py:

from kivy.app import App
from kivy.uix.widget import Widget

class SublimeLauncher(Widget):
    pass

class SublimeLauncherApp(App):
    def build(self):
        return SublimeLauncher()

if __name__ == "__main__":
    SublimeLauncherApp().run()

sublimelauncher.kv:

#:kivy 1.2.0
<SublimeLauncher>:
    FloatLayout:
        BoxLayout:
            orientation: 'vertical'
            spacing: 10
            Label:
                text: "Enter the path to the folder to open.
Press OK if you would like to open without a directory"
            TextInput:
                id: folderpath
            Button:
                text: 'OK'

I first tried it with just the BoxLayout, but read somewhere the root widget is always as big as the app. How do I declare the size of the app? Or the layout? How would you go about doing something like a dialog box?

Maybe I am missing something very basic, but I can't seem to figure it out.

Edit: here is what I am seeing..

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...