0
votes

I woud like to create ScrollView with custom Buttons (Label + Image), but I've got problem with proper positioning Label and Image on Button object.

Code:

class TestScreen(Screen):
    def change(self, btn, pos):
        print pos
    def populate(self):
        scroll = self.ids.myscroll
        grid = self.ids.scrollgrid
        btn =  Button(size_hint_y=None, width='29sp', text='')
        box = BoxLayout(size=btn.size, pos=btn.pos,orientation='horizontal')
        image = Image(source='image.png', size_hint_x=None, width=74)
        label = Label(size_hint_x=None, width=100, text='test')

        box.add_widget(image)
        box.add_widget(label)

        btn.add_widget(box)
        grid.add_widget(btn)

        btn.bind(pos=partial(self.change))

and .kv file:

<TestScreen>:
   Button:
       text: 'populate'
       size_hint: None, None
       size: 100,100
       pos: 0,0
       on_press: root.populate()
   ScrollView:
       id: myscroll
       size_hint: None, None
       size: 300, 500
       pos: 100, 100
       scroll_x: 0.5
       GridLayout:
          spacing: 20
          padding: 20
          id: scrollgrid
          size_hint: None, None
          cols: 1
           size_hint_y:  None
       children
        height: self.minimum_height
        width: self.parent.width

The biggest problem is that during creating BoxLayout in populate(), btn.pos is [0,0] and after rendering btn.pos is changed to proper coordinates (I checked that using bind() for pos). How can I get correct cooridnates during creating mentioned BoxLayout?

2

2 Answers

1
votes

If you add a widget to another widget, which isn't a layout, it will receive default pos (0,0) and size (100, 100). Use a grid layout to pack an image and a button together. Example:

main.py:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import Screen
from kivy.properties import StringProperty, ObjectProperty
from random import choice


class ImageButton(GridLayout):

    images = [
        'http://kivy.org/logos/kivy-logo-black-256.png',
        'http://img.interia.pl/rozrywka/nimg/2/7/roz4286600.jpg',
        'http://www.i-programmer.info/images/stories/News/2014/Apr/A/kivycont2.jpg',
        'http://static.giantbomb.com/uploads/scale_small/9/90155/2472244-abathur.png',
        'https://www.gravatar.com/avatar/a07de5a89d18964a22775deae84d9ba6?s=328&d=identicon&r=PG',
        'https://www.gravatar.com/avatar/578e323e1c4dda99b24bf047e0cb2e3e?s=328&d=identicon&r=PG&f=1'
    ]

    source = StringProperty(images[0])

    def change_image(self):
        while True:
            new_img = choice(self.images)
            if new_img != self.source:
                self.source = new_img
                break


class MainScreen(Screen):

    grid = ObjectProperty()

    def populate(self):
        for i in xrange(10):
            self.grid.add_widget(ImageButton())


class Test(App):

    def on_start(self):
        self.root.populate()


Test().run()

test.kv:

MainScreen:
    grid: grid

    ScrollView:

        GridLayout:
            id: grid
            cols: 1
            spacing: '10dp'
            padding: '10dp'
            size_hint_y: None
            height: self.minimum_height


<ImageButton>:
    cols: 2
    size_hint_y: None
    height: '200dp'

    AsyncImage:
        source: root.source
        size_hint_x: 0.3

    Button:
        text: 'click me'
        on_press: root.change_image()
0
votes

Adding widgets to a Button isn't a very nice solution. Button actually is a Label with a behavior set to it, therefore you only duplicate it without any good reason. How to make such a widget(a button with an image) is explained here, you'd need only to choose a position for Image and position whole widget for your needs. In the first example the App class also behaves as a BoxLayout, so imagine it as a layout only - a root widget. From there it'll be easy.

Also, when a widget is initialized, it comes with the default values of size and pos and other values specific for the widget itself, which are then changed. Basically, you created a widget with btn = Button(), but didn't set any pos or size, so when you access the position later within box, you only take the defaults and well... place them to size and pos of another widget - so you only replaced defaults with the same values. To get correct values you'll need to set them first inside the widget to your desired pos&size.