Anchor Layout only works in bottom left corner

132 views
Skip to first unread message

Ben Lawton

unread,
Sep 26, 2021, 7:53:48 AM9/26/21
to Kivy users support
I'm new to Kivy and am just trying to make a simple app. I have set a background using the canvas feature and then I am trying to add a widget in the centre of the screen using the anchor layout. However, when I try and anchor the widget It anchors it to a tiny portion of the screen. Here is the code and what it does:

The .py file:

Capture1.PNG

The .kv file:

Capture2.PNG

And what it does:

Capture3.PNG


Elliot Garbus

unread,
Sep 26, 2021, 8:50:10 AM9/26/21
to kivy-...@googlegroups.com

Your root widget, BackgroundImage is derived from Widget. Change this to a Layout, you could remove this widget, and make the AnchorLayout the root widget.  You can also instance with root widget in kv.

 

If you need more help and for the future, please attach or paste in code, not pictures of code.

 

From: Ben Lawton
Sent: Sunday, September 26, 2021 4:53 AM
To: Kivy users support
Subject: [kivy-users] Anchor Layout only works in bottom left corner

 

I'm new to Kivy and am just trying to make a simple app. I have set a background using the canvas feature and then I am trying to add a widget in the centre of the screen using the anchor layout. However, when I try and anchor the widget It anchors it to a tiny portion of the screen. Here is the code and what it does:

 

The .py file:

 

 

The .kv file:

 

 

And what it does:

 

 

 

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/1c547a06-3f31-4157-ad25-f544eb90b03cn%40googlegroups.com.

 

Capture2.PNG
Capture1.PNG
Capture3.PNG

ElliotG

unread,
Sep 26, 2021, 9:14:48 AM9/26/21
to Kivy users support
Here is a small example that has the root widget in kv.  I have used the Builder to load the kv code.  You can move this to a seperate file if you prefer.  I find for short examples it is convenient to have everything in one file.

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

from random import choice

kv = """
<JumpButtonLayout>:
    canvas:
        Color:
            rgb: .7, .7, .7
        Rectangle:
            size: self.size
            pos: self.pos
    anchor_x: 'center'
    anchor_y: 'top'
    Button:
        size_hint: None, None
        size: 150, 48
        text: 'Press to Position'
        on_release: root.random_pos()

BoxLayout:
    orientation: 'vertical'
    JumpButtonLayout:
    Label:
        text: 'Anchor Layout Test'
        size_hint_y: None
        height: 48
"""


class JumpButtonLayout(AnchorLayout):
    def random_pos(self):
        self.anchor_x = choice(['left', 'center', 'right'])
        self.anchor_y = choice(['top', 'center', 'bottom'])


class AnchorEvalApp(App):
    def build(self):
        return Builder.load_string(kv)


AnchorEvalApp().run()


Reply all
Reply to author
Forward
0 new messages