AnchorLayout seems to move everything to bottom left corner

19 views
Skip to first unread message

Tom Swirly

unread,
Mar 18, 2019, 7:10:22 AM3/18/19
to Kivy users support
Greetings, Kivers!  Thanks for a great library.  I'm porting an application from tk, and a problem came up./

The goal 

I want to position three widgets within another widget.

The three widgets are:

* A fixed size "enable" Switch on the top left
* A fixed size Label in the top middle
* A resizable widget (a level display) occupying the whole rest of the widget

and they should look something like this


<enable switch>         <A Big Label Here>


           
<some fancy widget fills the rest of the space>




The widget is called OneLaser and I'll later have four of these on a page called FourLasers.


What went wrong

I tried to use AnchorLayout just to sketch it out, but got unintuitive results.  

I have read several documents about it, and understand that a single AnchorLayout positions all its children using the same rule, so I used three different AnchorLayouts, and yet all of them moved the children to the bottom left corner.

I created a minimal example that demonstrates this on my machine (with a fresh install of kivy and Python 3.6.6, MacOS 10.12.6/Darwin 16.7.0).

class UIApp(App):
    def build(self):
        root = Widget(size_hint=(1, 1))

        tl = AnchorLayout(size_hint=(1, 1), anchor_y='top', anchor_x='left')
        tr = AnchorLayout(size_hint=(1, 1), anchor_y='top', anchor_x='right')
        bl = AnchorLayout(size_hint=(1, 1), anchor_y='bottom', anchor_x='left')

        tl.add_widget(Label(text='one'))
        tr.add_widget(Label(text='two'))
        bl.add_widget(Label(text='three'))

        root.add_widget(tl)
        root.add_widget(tr)
        root.add_widget(bl)
        return root


UIApp().run()


I experimented with this - for example, by adding and removing size_hints - but consistently all three labels appear in the bottom left corner.


My questions

1. How should AnchorLayout be used properly?

and really, more interesting to me:

2. What's the best way to accomplish my actual goal?

as I'm certainly not committed to using AnchorLayout.

Pedro Peres

unread,
Mar 18, 2019, 12:54:32 PM3/18/19
to Kivy users support
I'm somewhat new to this myself, so take this advice with a grain of salt but try replacing:
root = Widget(size_hint=(1, 1))
with:
root = FloatLayout()

Tom Swirly

unread,
Mar 18, 2019, 2:09:38 PM3/18/19
to kivy-...@googlegroups.com
That doesn't help me to understand AnchorLayout!  :-)



--
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 post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/09581f5a-03bf-4d7e-8aa0-c22d5487a23e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Elliot Garbus

unread,
Mar 19, 2019, 12:51:52 AM3/19/19
to kivy-...@googlegroups.com

To use the AnchorLayout (or any layout)  Make the root a Layout, not a widget.

I also recommend using KV Lang.  It is much more productive that just coding it all directly.

There is a great example in the example folder, called the Kivy Catalog that has examples of all of the layouts, and the KV code used in the example.  You can edit the KV code and see the results in the app interactively.

In order to create the look I believe you are going for I would not choose to use an AnchorLayout .  I would use a set of nested BoxLayouts.  I have also attached the code.

from kivy.app import App
from kivy.lang import Builder

kv_test = '''
BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        size_hint_y: .1
        Switch:
        Label:
            text: 'Your Big Label'
        Label:
            text: 'Make this blank'   # replace this with text:'' this is a placeholder for the layout to center your Label
    Button:
        text: 'The rest goes here'    
    
    '''

class TestLayoutApp(App):

    def build(self):
        return Builder.load_string(kv_test)

TestLayoutApp().run()

and the output looks like:

--
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 post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
testlayout.py

Tom Swirly

unread,
Mar 19, 2019, 4:06:44 AM3/19/19
to kivy-...@googlegroups.com
Ah, thanks!  I should have thought of that...  and thanks for a really nice example that looks exactly like what I wanted.

Yes, I do do KV lang, but I wanted to make the example minimal!  :-)


For more options, visit https://groups.google.com/d/optout.

cosbon jason,

unread,
Mar 19, 2019, 4:36:11 AM3/19/19
to Kivy users support
use an ActionBar then divide this action bar into three

ActionBar:
     ActionView:
            ActionPrevious:
                 StackLayout:
                       size_hint: (.2, .2)
                       orientation: 'rl-tb'
                       Button:
                           id: enable_switch
                           text: 'enable switch '
                           size_hint: (.1, .1)
                      Button:
                           id: Big_label_here
                           text: 'Big_label_here'
                           size_hint: (.1, .1)
                     Button:
                          id: some_fancy
             text: 'some_fancy'

Tom Swirly

unread,
Mar 19, 2019, 4:41:00 AM3/19/19
to kivy-...@googlegroups.com
Wow, I didn't even know about ActionBar!  Very interesting, and perhaps even closer to what I want.

Also, sounds like a place I'd like to go drinking.  :-D

--
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 post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages