我正在尝试构建一个基本的Kivy应用。添加基本元素并运行应用程序后,所有元素都塞满了左下角。它在Android和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.\nPress OK if you would like to open without a directory"
TextInput:
id: folderpath
Button:
text: 'OK'
我首先尝试使用BoxLayout,但在某处读取根小部件总是与应用程序一样大。如何声明应用程序的大小?还是布局?你会怎么做像对话框这样的事情?
也许我错过了一些非常基本的东西,但我似乎无法弄明白。
编辑:这就是我所看到的......
答案 0 :(得分: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)
答案 1 :(得分:5)
我最近写了a post我在编程接口时使用的一个小技巧。该技巧将让您在添加到屏幕的所有小部件(包括布局)周围看到边框。这将是您的代码的结果:
它利用继承和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'
答案 2 :(得分:4)
由于您的根小部件不是布局(您使SublimeLauncher
继承Widget
),因此它不会设置其子大小/位置。因此,您的FloatLayout
具有默认值,因为您也不会手动覆盖它们。
pos: 0, 0
size: 100, 100
这些默认值当然会限制孩子,因为FloatLayout
基于他们的size_hint属性约束他们的大小。
你想给他们更多空间,正如Nykakin指出的那样。
此外,由于您的文本大于Label(您没有设置halign和text_size),其纹理将以Label的中心为中心,因此它的某些部分位于屏幕之外。你想看看kivy / examples / widgets / textalign.py