6

我正在尝试构建一个基本的 Kivy 应用程序。添加基本​​元素并运行应用程序后,所有元素都挤在左下角。它在 android 和 Linux 上显示如下。

主要.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()

升华启动器.kv:

#:kivy 1.2.0
<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'

我第一次尝试只使用 BoxLayout,但在某处读取根小部件始终与应用程序一样大。如何声明应用程序的大小?还是布局?你会如何去做一个像对话框这样的事情?

也许我错过了一些非常基本的东西,但我似乎无法弄清楚。

编辑:这就是我所看到的..

在此处输入图像描述

4

3 回答 3

5

我最近写了一篇我在编程接口时使用的小技巧的帖子。该技巧将让您看到添加到屏幕的所有小部件(包括布局)周围的边框。这将是您的代码的结果:

在此处输入图像描述

它利用继承和 Kivy 规则来覆盖所有小部件的基类。您只需添加:

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

在这个文件的开头:

<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'
于 2013-10-08T08:31:27.900 回答
5

您的布局的默认大小为 100x100 像素。您可以尝试为它着色以查看它需要多少空间:

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()

设置非默认大小:

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)

占满空间:

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)
于 2013-10-07T05:06:21.433 回答
4

由于您的根小部件不是布局(您创建了SublimeLauncherinherit Widget),因此它不会设置其子级大小/位置。所以你FloatLayout有默认值,因为你也没有手动覆盖它们。

pos: 0, 0
size: 100, 100

这些默认值当然会限制孩子,因为FloatLayout通过基于他们的 size_hint 属性来限制他们的大小。

正如 Nykakin 指出的那样,你想给他们更多的空间。

此外,由于您的文本大于标签(您也没有设置 halign 和 text_size),因此它的纹理以标签的中心为中心,因此它的某些部分超出了屏幕。你想看看 kivy/examples/widgets/textalign.py

于 2013-10-07T09:49:34.017 回答