Kivy: BoxLayout vs GridLayout

BoxLayout(orientation='vertical') vs. GridLayout(cols=1) :

They both do the same, no? Is there a reason for choosing one over the other?

+4
source share
1 answer

Differences include size and position.

In the general case, GridLayout ( cols: 1 ) will always contain elements in one column , while there is great flexibility for organizing individual widgets when using BoxLayout ( orientation: 'vertical' ) .

Here is a very simple example of what you can do with BoxLayout , because it honors pos_hint , size and size_hint (and others, such as center_x , x , y , right , - note that they also depend on the orientation of vertical or horizontal BoxLayout ), which affects individual widgets:

 < Test@BoxLayout >: orientation: 'vertical' Button: text: 'a' size_hint: None, None size: 100,50 pos_hint: { 'center_x' : .5 } Button: text: 'b' 

This is the output on the 200x200 screen:

BoxLayout with vertical orientation

If you try to do the same, but using GridLayout instead, you will get the following:

GridLayout with cols: 1

Finally, GridLayout has some properties for controlling column size:

  • col_default_width : for default width for all columns
  • col_width : a list of widths for each column (not useful in this because we only have one)
  • col_force_default : which ignores any existing size_hint or size for individual widgets and forces the column width
  • minimum_width : so the column is not shrinking too much
+12
source

Source: https://habr.com/ru/post/1496941/


All Articles