how do I clear widgets while keeping layout format

1,844 views
Skip to first unread message

Drew Personal

unread,
Sep 21, 2015, 11:26:39 PM9/21/15
to Kivy users support
I have my kivy file like so:

#: kivy 1.9.0

<Gui>:
    grid: grid_layout_1
    BoxLayout:
        orientation: 'horizontal'
        GridLayout:
            id: grid_layout_1
            cols: 10
            height: 500
            Image:
            Image:
            Image:
            Image:
            Image:
            Image:

I am trying to clear widgets like so when a button is pressed:

def clear_images(self, A):
    G = Gui()
    G.A.clear_widgets()
    ...

when I do this it seems to be clearing the id, cols, and height, how do I do this just removing all the images?

This is so all the images get replaced with a new album, with, more or less images than what is currently being viewed

Alexander Taylor

unread,
Sep 22, 2015, 9:22:16 AM9/22/15
to Kivy users support
I would manage the Images in python rather than in kv, it isn't convenient for dynamically replacing them like this.

niavlys

unread,
Sep 22, 2015, 10:15:41 AM9/22/15
to Kivy users support

according to clear_widget's doc:
clear_widgets(children=None)Added in 1.0.0

Remove all (or the specified) children of this widget. If the ‘children’ argument is specified, it should be a list (or filtered list) of children of the current widget.


something like this should work:


G.A.clear_widgets([i for i in G.A.children if type(i) is Image])

Drew Personal

unread,
Sep 22, 2015, 4:29:14 PM9/22/15
to Kivy users support


On Tuesday, September 22, 2015 at 7:22:16 AM UTC-6, Alexander Taylor wrote:
I would manage the Images in python rather than in kv, it isn't convenient for dynamically replacing them like this.


The Images are added to the grid layout, which is housed in a scroll view, by looping through a dict of Images that are added to the grid like so

for i in images.keys( ):
    G.grid.add_widget(Image(source='images/' + str(images[i]) + '.png'))

This dict gets swapped with other albums of images and need to replace the currently existing images.

Drew Personal

unread,
Sep 23, 2015, 8:25:04 PM9/23/15
to Kivy users support
 
I tried this but I get the same error,

[WARNING           ] <kivy.uix.gridlayout.GridLayout object at 0x00000000097E3A6
8> have no cols or rows set, layout is not triggered.
[WARNING           ] <kivy.uix.gridlayout.GridLayout object at 0x00000000097E3A6
8> have no cols or rows set, layout is not triggered.

When I clear_widgets() or clear_widgets([i for i in G.A.children if type(i) is Image]), both return this.

ZenCODE

unread,
Sep 24, 2015, 4:30:34 AM9/24/15
to Kivy users support
You could always try just changing the 'source' property of the images, Instead if removing them, you just change the images. Might be faster and easier?
Reply all
Reply to author
Forward
0 new messages