标签不受AnchorLayout的影响-Kivy [英] Label not affected by AnchorLayout - Kivy

查看:72
本文介绍了标签不受AnchorLayout的影响-Kivy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习奇异果,并被这种布局问题所困扰. 我使用锚点布局"使用以下命令将按钮居中:

I have just started learning kivy and am stuck with this layout problem. I used the Anchor Layout to center a button using:

Button: text: "A button" anchor_x:'center' anchor_y:'center'

Button: text: "A button" anchor_x:'center' anchor_y:'center'

但是,当我尝试此操作时:

However when I try this:

Label:
    text: "Hello World!"
    size_hint: 1, 1
    anchor_x:'center'
    anchor_y:'top'

它仅显示"Hello World!"设置为默认居中位置,但我希望它位于居中按钮上方.

It just shows "Hello World!" in the default center position but I want it above my centered button.

我已经尝试了所有内容并查看了文档,但没有发现任何可以解释的信息.

I have tried everything and looked at the docs but have found nothing to explain this.

这是两个文件:

py文件:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout

class Layout(AnchorLayout):
    pass

class LayoutApp(App):
    def build(self):
        return Layout()

if __name__ == '__main__':
    LayoutApp().run()

kv文件:

<Layout>:
    Label:
        text: "Hello World!"
        font_size: 50
        size_hint: None, None
        height: 100
        anchor_x:'center'
        anchor_y:'top'

    Button:
        text: "Button"
        background_colour: (0,0,1,1)
        size_hint: None, None
        size: 50, 50
        anchor_x:'center'
        anchor_y:'top'

推荐答案

size_hint:1、1

size_hint: 1, 1

这是您的问题,size_hint表示Label与父版面的大小相同,中间是文本.屏幕中间.标签的锚定位置无关紧要,因为它没有移动的空间.

This is your problem, the size_hint means the Label is the same size as the parent layout, with the text in the middle...i.e. the middle of the screen. It doesn't matter where the label is anchored, because there's no room for it to move.

您应该手动设置所需的尺寸,例如

You should instead manually set the size you want, e.g.

size_hint_y:  None
height: 50

这篇关于标签不受AnchorLayout的影响-Kivy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆