Skip to content Skip to sidebar Skip to footer

Is It Possible In Kivy Gridlayout To Specify A Particular Grid For A Particular Widget

I have been using PyQt since a long time and i know that if we use QGridLayout in PyQt we can specify a particular widget at a particular grid. for example grid = QtGui.QGridLayo

Solution 1:

In Kivy, when we add widgets to a GridLayout, we just specify either the rows, columns or both.

We then leave the positioning to Kivy which automatically fills it as per our specifications.

Look at Official Documentation for GridLayout

There is a really nice .gif which explains how widgets are arranged in a GridLayout.

So, the answer to your question is NO. We cannot specify where we want to place a widget, however, all that we can do is add widgets to the GridLayout in such a manner that it positions it correctly.

Initially, I found difficulties in making a widget occupy more space than once cell, like this:

<2 cells><1cell>
+--------+-----+
|Big     |     |
|Widget  |     |
+--------+-----+
|   |    |     |
|   |    |     |
+---+----+-----+

However, this can be solved making the entire row a BoxLayout. Like so:

<1 big BoxLayout>
+--------+-----+
|Big     |     |
|Widget  |     |
+--------+-----+
|   |    |     |
|   |    |     |
+---+----+-----+
<1 more BoxLayout>

This way, each BoxLayout occupies only one cell. The spacing of the widgets can be customized with the BoxLayout and then be placed in the Grid.

Here's how to do that in Kv:

<MyGrid>cols=1# Actually, you can just use a vertical boxBoxLayout:BigWidget:# PropertiesSmallWidget:# PropertiesBoxLayout:SmallWidget:# PropertiesSmallWidget:# PropertiesSmallWidget:# Properties

Kivy will automatically fill the Grid with 1 column

So, by nesting layouts you can get the desired output.

Solution 2:

Kivy's GridLayout is simply different to PyQt's, and isn't suitable for this particular purpose.

A natural alternative would be to use a FloatLayout, placing widgets via pos_hint and size_hint. You could even create a FloatLayout subclass that automatically handles positioning in a grid.

As it happens, I've previously created exactly this, you can find my code here. My SparseGridLayout uses a child widget's col and row attributes to position it appropriately. You could also override add_widget to precisely duplicate the qt syntax if you like, but the important thing to understand is how I have used a FloatLayout to place widgets.

Post a Comment for "Is It Possible In Kivy Gridlayout To Specify A Particular Grid For A Particular Widget"