willmac
willmac

Reputation: 1627

Image in gridlayout kivy

I am trying to create a 10x10 grid of images in kivy but I find that all the images are separated by a large black border.It appears that the images remain at a constant 0f 100x100 pixels.

The code I have used so far does not include a .kv file:

class SkyBox(BoxLayout):

    def update(self):

        for i in range(100):
            self.children[len(self.children)-1].add_widget(Image(source=random.choice(["rock.png","rock2.png"]))

class SkyGame(Widget):
    def run(self):
        box=SkyBox()

        box.add_widget(GridLayout(cols=10))

        box.update()

        return box

class SkyApp(App):

    def build(self):
        return SkyGame().run()


if __name__=="__main__":
    SkyApp().run()

It must also be noted that my end result requires multiple boxlayouts to be created in the update function.

Upvotes: 1

Views: 2637

Answers (1)

inclement
inclement

Reputation: 29460

self.children[len(self.children)-1]

This can be written more concisely and clearly as self.children[-1].

add_widget(Image(source=random.choice(["rock.png","rock2.png"]))

The image widget does not by default stretch the image beyond its real dimensions (in this case, apparently 100x100). To change this, simply set its allow_stretch property:

 add_widget(Image(source=random.choice(["rock.png","rock2.png"], allow_stretch=True))

If you want to allow it to stretch by different amounts in different directions, you can add keep_ratio=False as well.

Upvotes: 1

Related Questions