Dynamically adjusting BoxLayout

171 views
Skip to first unread message

Carry White

unread,
May 3, 2014, 7:11:07 AM5/3/14
to kivy-...@googlegroups.com
I'm trying to get an image into my Boxlayout. I want this image to fill all of the screen width and I want it to keep aspect ratio. In the remaing space at the bottom of the screen I want to have a button, which fills all of the remaining space. To illustrate what I'm trying to achive I made this little doodle:



So I want the img_width to be = screen_width.
I want the img_height to be the size it has to be, to keep aspect ratio.

I want the button width to be = screen_width
And I want the button height to take up the remaining height. So I want button_height = screen_height-button_height

I tried many things but I simple could not get what I want. This is an example of what I tried:

Code hier eingeben.from kivy.app import App

from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout

from kivy.uix.image import Image


class MainImage(BoxLayout):
   
pass
       

class KommaApp(App):
   
def build(self):
        a
= MainImage()
       
return a
   
if __name__ == '__main__':
   
KommaApp().run()..

Code hier eingeben.
<MainImage@BoxLayout>:
    orientation
: 'vertical'
    width
: main_image.width
    height
: main_image.height
   
Image:
        id
: main_image
        source
: '02.jpg'
   
Button:
        text
: 'Button A'    
       
..

Any help on this would be appreciated :) Thanks

ZenCODE

unread,
May 26, 2014, 3:14:28 PM5/26/14
to kivy-...@googlegroups.com
Sorry for the late reply, but better late than never I hope? ;-)

So, I think this doe what you want.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string('''
#<MainImage@BoxLayout>: #  No need to repeat the BoxLayout subclassing
<MainImage>:
    img: img
    btn: btn
    orientation: '
vertical'
    Image:
        id: img
        source: '
img1.png'
        size_hint: (None, None)
        allow_stretch: True
        keep_ratio: False

    Button:
        id: btn
        text: '
Button A'
        size_hint_y: None
'''
)


class MainImage(BoxLayout):

   
def on_size(self, widget, size):
       
self.img.size = (self.width, self.width * self.img.image_ratio)
       
self.btn.height = self.height - self.img.height


class KommaApp(App):
   
def build(self):
        a
= MainImage()
       
return a

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

It seems a bit awkward to me, and certainly not the best use of layouts. I would have thought this would work better, but then again, I have no idea what you are actually trying to do, so feel free to think me a petulant prat...;-)

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string('''
#<MainImage@BoxLayout>: #  No need to repeat the BoxLayout subclassing
<MainImage>:
    img: img
    btn: btn

    orientation: '
vertical'
    FloatLayout:
        Image:
            id: img
            source: '
img1.png'
            allow_stretch: True
            keep_ratio: False
            size_hint: (None, None)
            pos_hint: {"center_x": 0.5, "center_y": 0.5}

    Button:
        id: btn
        text: '
Button A'
        size_hint_y: None
        height: "30sp"
'''
)


class MainImage(BoxLayout):

   
def on_size(self, widget, size):
        area_ratio
= self.width / (self.height - self.btn.height)
        img
= self.img
       
if area_ratio < img.image_ratio:
            img
.size = (self.width, self.width * img.image_ratio)
       
else:
            img
.size = (self.width / area_ratio,
                       
self.height - self.btn.height)
Reply all
Reply to author
Forward
0 new messages