gpt4 book ai didi

python - Kivy - 基础应用程序有奇怪的对齐方式

转载 作者:太空狗 更新时间:2023-10-29 22:09:34 30 4
gpt4 key购买 nike

我正在尝试构建一个基本的 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()

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,但在某处读到根小部件总是与应用程序一样大。如何声明应用程序的大小?还是格局?您将如何做类似对话框的事情?

也许我遗漏了一些非常基本的东西,但我似乎无法弄明白。

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

enter image description here

最佳答案

您的布局的默认大小为 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)

关于python - Kivy - 基础应用程序有奇怪的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19215337/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com