GridView

class vanilla.GridView(posSize, contents, columnWidth=None, columnSpacing=0, columnPadding=(0, 0), columnPlacement='leading', rowHeight=None, rowSpacing=0, rowPadding=(0, 0), rowPlacement='top', rowAlignment='firstBaseline', columnDescriptions=None)

A view that allows the placement of other views within a grid.

../_images/GridView.png
from vanilla import Button, GridView, Window

class GridViewExample:
    def __init__(self):
        self.w = Window((120, 90))

        self.button1 = Button("auto", "one")
        self.button2 = Button("auto", "two")
        self.button3 = Button("auto", "three")
        self.button4 = Button("auto", "four")

        self.w.gridView = GridView(
            (0, 0, 0, 0),
            contents=[
                dict(
                    cells=[
                        dict(view=self.button1),
                        dict(view=self.button2),
                    ]
                ),
                dict(
                    cells=[
                        dict(view=self.button3),
                        dict(view=self.button4),
                    ]
                ),
            ],
            columnPadding=(4, 4),
            rowPadding=(4, 4),
        )

        self.w.open()


GridViewExample()

posSize Tuple of form (left, top, width, height) or “auto” representing the position and size of the grid view.

contents The contents to display within the grid. See below for structure.

columnWidth The width for columns.

columnSpacing The amount of spacing between columns.

columnPadding The (left, right) padding for columns.

columnPlacement The horizontal placement of content within columns. Options:

  • “leading”
  • “center”
  • “trailing”
  • “fill”

rowHeight The height for rows.

rowSpacing The amount of spacing between rows.

rowPadding The (top, bottom) padding for rows.

rowPlacement The vertical placement of content within rows. Options:

  • “top”
  • “center”
  • “bottom”
  • “fill”

rowAlignment The alignment of the row. Options:

  • “firstBaseline”
  • “lastBaseline”
  • “none”

columnDescriptions An optional list of dictionaries defining specific attributes for the columns. Options:

  • “width”
  • “columnPadding”
  • “columnPlacement”

Contents Definition Structure

Contents are defined as with a list of row definitions. A row definition is a list of cell definitions or a dictionary with this structure:

  • cells A list of cell definitions.
  • rowHeight (optional) A height for the row that overrides the GridView level row height.
  • rowPadding (optional) A (top, bottom) padding definition for the row that overrides the GridView level row padding.
  • rowPlacement (optional) A placement for the row that overrides the GridView level row placement.
  • rowAlignment (optional) An alignment for the row that overrides the GridView level row placement.

Cells are defined with either a Vanilla object, a NSView (or NSView subclass) object, None, or a dictionary with this structure:

  • view A Vanilla object or NSView (or NSView subclass) object.
  • width (optional) A width to apply to the view.
  • height (optional) A height to apply to the view.
  • columnPlacement (optional) A horizontal placement for the cell that overrides the row level or GridView level placement.
  • rowPlacement (optional) A vertical placement for the cell that overrides the row level or GridView level placement.
  • rowAlignment (optional) A row alignment for the cell that overrides the row level or GridView level alignment.

If a cell is defined as None, the cell will be merged with the first cell directly above that has content.

addAutoPosSizeRules(rules, metrics=None)

Add auto layout rules for controls/view in this view.

rules must be a list of rule definitions. Rule definitions may take two forms:

key value
“view1” The vanilla wrapped view for the left side of the rule.
“attribute1” The attribute of the view for the left side of the rule. See below for options.
“relation” (optional) The relationship between the left side of the rule and the right side of the rule. See below for options. The default value is “==”.
“view2” The vanilla wrapped view for the right side of the rule.
“attribute2” The attribute of the view for the right side of the rule. See below for options.
“multiplier” (optional) The constant multiplied with the attribute on the right side of the rule as part of getting the modified attribute. The default value is 1.
“constant” (optional) The constant added to the multiplied attribute value on the right side of the rule to yield the final modified attribute. The default value is 0.

The attribute1 and attribute2 options are:

value AppKit equivalent
“left” NSLayoutAttributeLeft
“right” NSLayoutAttributeRight
“top” NSLayoutAttributeTop
“bottom” NSLayoutAttributeBottom
“leading” NSLayoutAttributeLeading
“trailing” NSLayoutAttributeTrailing
“width” NSLayoutAttributeWidth
“height” NSLayoutAttributeHeight
“centerX” NSLayoutAttributeCenterX
“centerY” NSLayoutAttributeCenterY
“baseline” NSLayoutAttributeBaseline
“lastBaseline” NSLayoutAttributeLastBaseline
“firstBaseline” NSLayoutAttributeFirstBaseline

Refer to the NSLayoutAttribute documentation for the information about what each of these do.

The relation options are:

value AppKit equivalent
“<=” NSLayoutRelationLessThanOrEqual
“==” NSLayoutRelationEqual
“>=” NSLayoutRelationGreaterThanOrEqual

Refer to the NSLayoutRelation documentation for the information about what each of these do.

metrics may be either None or a dict containing key value pairs representing metrics keywords used in the rules defined with strings.

appendColumn(cells, columnWidth=None, columnPadding=None, columnPlacement=None)

Append a column and populate it with a list of cells. The cells must have the same structure as defined in __init__.

appendRow(cells, rowHeight=None, rowPadding=None, rowPlacement=None, rowAlignment=None)

Append a row and populate it with a list of cells. The cells must have the same structure as defined in __init__.

Merging is not possible with this method.

columnIsVisible(index)

Get the visibility of column at index.

enable(onOff)

Enable or disable the object. onOff should be a boolean.

getColumnCount()

Get the number of columns.

getPosSize()

The position and size of the object as a tuple of form (left, top, width, height).

getRowCount()

Get the number of rows.

insertColumn(index, cells, columnWidth=None, columnPadding=None, columnPlacement=None)

Insert a column at index and populate it with a list of cells. The cells must have the same structure as defined in __init__.

insertRow(index, cells, rowHeight=None, rowPadding=None, rowPlacement=None, rowAlignment=None)

Insert a row at index and populate it with a list of cells. The cells definition must have the same structure as defined in __init__.

Merging is not possible with this method.

isVisible()

Return a bool indicating if the object is visible or not.

move(x, y)

Move the object by x units and y units.

moveColumn(fromIndex, toIndex)

Move column at fromIndex to toIndex.

moveRow(fromIndex, toIndex)

Move row at fromIndex to toIndex.

removeColumn(index)

Remove column at index.

removeRow(index)

Remove row at index.

resize(width, height)

Change the size of the object to width and height.

rowIsVisible(index)

Get the visibility of row at index.

setPosSize(posSize, animate=False)

Set the position and size of the object.

posSize A tuple of form (left, top, width, height).

animate A boolean flag telling to animate the transition. Off by default.

show(onOff)

Show or hide the object.

onOff A boolean value representing if the object should be shown or not.

showColumn(index, value)

Set the visibility of column at index.

showRow(index, value)

Set the visibility of row at index.