Kivy的锚点位置不正确。

huangapple go评论57阅读模式
英文:

Kivy AnchorPoint is not in the right place

问题

我正在尝试将文本置于窗口顶部居中,但最终它被截断在左下角。

我有以下Python代码:

# anchortest.py

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

class Root(Widget):
    pass

class TextTitle(AnchorLayout):
    pass

class AnchorTestApp(App):
    def build(self):
        return Root()
    
AnchorTestApp().run()

以及它的关联kv文件。

# anchortest.kv

<Root>
    TextTitle:

<TextTitle>
    anchor_x: 'center'
    anchor_y: 'top'

    Label:
        id: score
        text: 'Hello World'
        font_name: 'Courier New'
        font_size: 40
        pad_x: 10
        pad_y: 10

当我运行应用程序时,我得到了这个:

Kivy的锚点位置不正确。

英文:

I am trying to just put a piece of text centred in the top on the window but it ends up truncated in the bottom left.

I have the following python code:

# anchortest.py

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

class Root(Widget):
    pass

class TextTitle(AnchorLayout):
    pass

class AnchorTestApp(App):
    def build(self):
        return Root()

AnchorTestApp().run()

and it's associated kv file.

# anchortest.kv

&lt;Root&gt;
    TextTitle:

&lt;TextTitle&gt;
    anchor_x: &#39;center&#39;
    anchor_y: &#39;top&#39;

    Label:
        id: score
        text: &#39;Hello World&#39;
        font_name: &#39;Courier New&#39;
        font_size: 40
        pad_x: 10
        pad_y: 10

When I run the app I get this:

Kivy的锚点位置不正确。

答案1

得分: 1

不要使用 AnchorLayout,只需使用 BoxLayout 并添加以下代码:

pos_hint: {'top': 1}
size_hint_y: None
from kivy.app import App
from kivy.lang.builder import Builder

KV = """   
Screen:
    BoxLayout:
        orientation: 'vertical'
        
        BoxLayout:
            pos_hint: {'top': 1}
            size_hint_y: None
            
            canvas.before:
                Color:
                    rgba: [0.5, 0, 1, 1]
                    
                Rectangle:
                    pos: self.pos
                    size: self.size
            
            Label:
                text: 'Hello World'
                font_size: sp(60)
                
        BoxLayout:
            Label:
                text: 'Screen text'
"""


class TestApp(App):
    def build(self):
        return Builder.load_string(KV)


TestApp().run()

你还可以使用 Label 的参数 valignhalign

KV = """   
Screen:
    Label:
        text: 'Hello World'
        font_size: sp(60)
        text_size: self.size
        halign: 'center'
        valign: 'top'
"""

如果你确实想要出于某种原因使用 AnchorLayout,可以这样做。根据 kivy 文档,为了使 AnchorLayout 的规则生效,必须为 Label 设置以下代码:

size_hint_y: None
height: self.height
KV = """ 
AnchorLayout:
    anchor_x: 'center'
    anchor_y: 'top'
            
    Label:
        text: 'Hello World'
        font_size: sp(60)
        size_hint_y: None
        height: self.height
"""
英文:

Don't use AnchorLayout for this case, just use BoxLayout with

pos_hint: {&#39;top&#39;: 1}
size_hint_y: None
from kivy.app import App
from kivy.lang.builder import Builder

KV = &quot;&quot;&quot;   
Screen:
    BoxLayout:
        orientation: &#39;vertical&#39;
        
        BoxLayout:
            pos_hint: {&#39;top&#39;: 1}
            size_hint_y: None
            
            canvas.before:
                Color:
                    rgba: [0.5, 0, 1, 1]
                    
                Rectangle:
                    pos: self.pos
                    size: self.size
            
            Label:
                text: &#39;Hello World&#39;
                font_size: sp(60)
                
        BoxLayout:
            Label:
                text: &#39;Screen text&#39;
&quot;&quot;&quot;


class TestApp(App):
    def build(self):
        return Builder.load_string(KV)


TestApp().run()

You also can use Label parametrs valign and halign:

KV = &quot;&quot;&quot;   
Screen:
    Label:
        text: &#39;Hello World&#39;
        font_size: sp(60)
        text_size: self.size
        halign: &#39;center&#39;
        valign: &#39;top&#39;
&quot;&quot;&quot;

> In order to control sizing, you must specify text_size to constrain
> the text and/or bind size to texture_size to grow with the text.

If you really want to use AnchorLayout for some reason, it's done this way. Following the kivy documentation, in order for the AnchorLayout rules to apply, the Label must be set with

size_hint_y: None
height: self.height

> The size_hint is a tuple of values used by layouts to manage the sizes
> of their children. It indicates the size relative to the layout’s size
> instead of an absolute size (in pixels/points/cm/etc)

KV = &quot;&quot;&quot; 
AnchorLayout:
    anchor_x: &#39;center&#39;
    anchor_y: &#39;top&#39;
            
    Label:
        text: &#39;Hello World&#39;
        font_size: sp(60)
        size_hint_y: None
        height: self.height
&quot;&quot;&quot;

huangapple
  • 本文由 发表于 2023年1月9日 02:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定