From 471112f9b4c454546cc4cc63fd2e6bf0472f7b06 Mon Sep 17 00:00:00 2001 From: Cory Laughlin Date: Tue, 23 Aug 2022 12:23:34 -0500 Subject: [PATCH] Added a global grid args for grid args common to all widgets in a template --- pyproject.toml | 2 +- tests/test_skel.py | 25 +++++++++++++++++++++++++ tklife/skel.py | 7 ++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cd774cd..235156b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "tklife" -version = "2.1.0-dev0" +version = "2.1.0-dev1" description = "Make Tk life easier" authors = ["Cory Laughlin "] license = "MIT" diff --git a/tests/test_skel.py b/tests/test_skel.py index 0942356..fe11027 100644 --- a/tests/test_skel.py +++ b/tests/test_skel.py @@ -141,6 +141,31 @@ def template(self): call(skeleton), call().grid(row=1, column=1, arg4=True), ] + def test_create_all_creates_and_grids_widgets_from_template_with_global_grid_args( + self, + mock_master, + mock_controller, + mock_mixin_class, + mocked_widget): + + class Tested(SkeletonMixin, mock_mixin_class): + @property + def template(self): + return ( + [SkelWidget(mocked_widget, {}, {'arg1': True}), SkelWidget( + mocked_widget, {}, {'arg2': True})], + [SkelWidget(mocked_widget, {}, {'arg3': True}), SkelWidget( + mocked_widget, {}, {'arg4': True})], + ) + skeleton = Tested(mock_master, mock_controller, {'garg': True}) + actual = mocked_widget.mock_calls + assert actual == [ + call(skeleton), call().grid(row=0, column=0, garg=True, arg1=True), + call(skeleton), call().grid(row=0, column=1, garg=True, arg2=True), + call(skeleton), call().grid(row=1, column=0, garg=True, arg3=True), + call(skeleton), call().grid(row=1, column=1, garg=True, arg4=True), + ] + def test_create_all_creates_and_grids_widgets_from_template_skipping_none( self, diff --git a/tklife/skel.py b/tklife/skel.py index 8768f13..f23fc7e 100644 --- a/tklife/skel.py +++ b/tklife/skel.py @@ -13,7 +13,7 @@ class SkelWidget(typing.NamedTuple): class SkeletonMixin(abc.ABC): - def __init__(self, master: tkinter.Misc, controller: 'ControllerABC', **kwargs) -> None: + def __init__(self, master: tkinter.Misc, controller: 'ControllerABC', global_grid_args=None, **kwargs) -> None: # Set the controller first self.controller = controller @@ -21,7 +21,7 @@ def __init__(self, master: tkinter.Misc, controller: 'ControllerABC', **kwargs) super().__init__(master, **kwargs) self.created: dict[str, dict] = {} - self.create_all() + self.create_all(global_grid_args if global_grid_args else {}) self.create_events() @property @@ -29,7 +29,7 @@ def __init__(self, master: tkinter.Misc, controller: 'ControllerABC', **kwargs) def template(self) -> typing.Iterable[typing.Iterable[SkelWidget]]: pass - def create_all(self): + def create_all(self, global_grid_args: dict): for row_index, row in enumerate(self.template): for col_index, skel_widget in enumerate(row): if skel_widget is None: @@ -40,6 +40,7 @@ def create_all(self): w = skel_widget.widget(self, **skel_widget.init_args) w.grid(row=row_index, column=col_index, + **global_grid_args, **skel_widget.grid_args) if skel_widget.label is not None: # And what is the vardict?