【发布时间】:2019-06-26 19:31:11
【问题描述】:
我想在 .kv 文件中构建以下简单设计。
它由三部分组成:
- 一个左上角的锚布局,由 3 列的网格布局组成。我希望它的宽度等于窗口宽度的 20%,高度等于窗口高度的 75%。
- 一个右上角的锚布局,由垂直方向的框布局组成。我希望它的宽度等于窗口宽度的 80%,高度等于窗口高度的 75%。
- 一个左下角的锚布局,目前是空的。我希望它的高度是窗口高度的 25%。
这三个部分本身包含在 AnchorLayout 中。
我尝试将这个设计翻译成 .kv 文件,如下所示。
#:kivy 1.11.1
<Example>:
anchor_x: "center"
anchor_y: "center"
AnchorLayout:
anchor_x: "left"
anchor_y: "top"
size_hint: (0.2, 0.75)
GridLayout:
cols: 3
Button:
text: "X"
Button:
text: "X"
Button:
text: "X"
Button:
text: "X"
Button:
text: "X"
Button:
text: "X"
AnchorLayout:
anchor_x: "right"
anchor_y: "top"
size_hint: (0.8, 0.75)
BoxLayout:
orientation: "vertical"
Label:
text: "HELLO..."
Label:
text: "WORLD..."
AnchorLayout:
anchor_x: "left"
anchor_y: "bottom"
size_hint: (1, 0.25)
Label:
text: "FOOTER"
如果重要的话,这里也是我的 .py 文件的代码。
# Importing Kivy
import kivy
kivy.require("1.11.1")
# Importing kivy libraries
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang import Builder
# Importing external libraries
# Import kv files
Builder.load_file("example.kv")
# Root widget of the application
class Example(AnchorLayout):
pass
# Application class
class TestApp(App):
def build(self, **kwargs):
return Example()
# Launch the application
if __name__=="__main__":
app = TestApp()
app.run()
输出看起来不像我预期的那样,如下图所示:
我不明白。因为 AnchorLayout 是 Widget 类的子类,并且它本身包含在 Layout 中,所以它的 size_hint 属性应该可以让我定义它的大小。
我在这里缺少什么?提前致谢!
【问题讨论】:
-
AnchorLayout的anchor_x和anchor_y属性会影响AnchorLayout的子代,因此您的Example布局试图将其所有子代靠近中心。 -
非常感谢,这完全合乎逻辑!
标签: python-3.x layout kivy kivy-language sizing