From 62ef55637df67282934ff5fd6a502c373266d910 Mon Sep 17 00:00:00 2001 From: insolor Date: Sun, 19 Jun 2022 00:08:54 +0300 Subject: [PATCH] Add row_span, configure to Row, change kwarg precedence --- tk_grid_helper/tk_grid_helper.py | 42 +++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/tk_grid_helper/tk_grid_helper.py b/tk_grid_helper/tk_grid_helper.py index 5f69a49..8f0b3e1 100644 --- a/tk_grid_helper/tk_grid_helper.py +++ b/tk_grid_helper/tk_grid_helper.py @@ -16,34 +16,45 @@ def __init__(self, widget: tk.Widget, column_index: int, row_index: int, class Row: + grid: "Grid" row_index: int column_index: int cells: List[Cell] - - def __init__(self, row_index: int): + + def __init__(self, grid: "Grid", row_index: int): + self.grid = grid self.row_index = row_index self.column_index = 0 self.cells = list() - + def add(self, widget: tk.Widget, **kwargs) -> "Row": # -> self if self.cells: self.column_index += self.cells[-1].column_span - + self.cells.append(Cell( widget, self.column_index, self.row_index, **kwargs, )) - + return self - - def column_span(self, span: int): # --> Self + + def column_span(self, span: int): # -> Self if self.cells: self.cells[-1].column_span = span - + + return self + + def row_span(self, span: int): # -> Self + if self.cells: + self.cells[-1].row_span = span + return self + def configure(self, *args, **kwargs): + self.grid.parent.grid_rowconfigure(self.row_index, *args, **kwargs) + class Grid: rows: List[Row] @@ -56,7 +67,7 @@ def __init__(self, parent, **kwargs): self.kwargs = kwargs def new_row(self) -> Row: - row = Row(self.row_index) + row = Row(self, self.row_index) self.rows.append(row) self.row_index += 1 return row @@ -70,15 +81,18 @@ def rowconfigure(self, i, *args, **kwargs): def build(self): for row in self.rows: for cell in row.cells: - kwargs = self.kwargs - kwargs.update(cell.kwargs) - cell.widget.grid( + # Common kwargs have the lowest priority + kwargs = self.kwargs.copy() + # Then go parameters set by coll_span() and row_span() + kwargs.update(dict( column=cell.column_index, row=cell.row_index, columnspan=cell.column_span, rowspan=cell.row_span, - **kwargs, - ) + )) + # Parameters of add() override all the previous parameters + kwargs.update(cell.kwargs) + cell.widget.grid(**kwargs) class DefaultRootWrapper: # pragma: no cover