コンテンツにスキップ

pubsubtk.ui.base.template_base

pubsubtk.ui.base.template_base

複数スロットを持つテンプレート UI を構築するための基底クラス。

TemplateMixin

TemplateMixin(store: Store[TState], *args, **kwargs)

Bases: ABC, Generic[TState]

テンプレートコンポーネント用のMixin。

複数のスロット(区画)を定義し、各スロットに独立してコンポーネントを配置できる。 ヘッダー・フッターなど固定部分と可変部分を分離したレイアウトを実現。

Note

テンプレート自体は状態を持たず、レイアウト定義とスロット管理のみを行う。 各スロットに配置されるコンポーネントが独自に状態管理を行う。

Mixin の初期化処理。

Source code in src/pubsubtk/ui/base/template_base.py
def __init__(self, store: Store[TState], *args, **kwargs):
    """Mixin の初期化処理。"""

    self.store = store
    self._slots: Dict[str, tk.Widget] = {}
    self._slot_contents: Dict[str, tk.Widget] = {}

    # テンプレートのセットアップ
    self.setup_template()
    self._slots = self.define_slots()

setup_template

setup_template() -> None

テンプレート固有の初期化処理(必要に応じてオーバーライド)。 define_slots()の前に呼ばれる。

Source code in src/pubsubtk/ui/base/template_base.py
def setup_template(self) -> None:
    """
    テンプレート固有の初期化処理(必要に応じてオーバーライド)。
    define_slots()の前に呼ばれる。
    """
    pass

define_slots abstractmethod

define_slots() -> Dict[str, Widget]

スロット(区画)を定義する。

Returns:

Type Description
Dict[str, Widget]

Dict[str, tk.Widget]: {"スロット名": フレームWidget} の辞書

Example
ヘッダー

self.header_frame = tk.Frame(self, height=60, bg='navy') self.header_frame.pack(fill=tk.X)

メインコンテンツ

self.main_frame = tk.Frame(self) self.main_frame.pack(fill=tk.BOTH, expand=True)

フッター

self.footer_frame = tk.Frame(self, height=30, bg='gray') self.footer_frame.pack(fill=tk.X)

return { "header": self.header_frame, "main": self.main_frame, "footer": self.footer_frame }

Source code in src/pubsubtk/ui/base/template_base.py
@abstractmethod
def define_slots(self) -> Dict[str, tk.Widget]:
    """
    スロット(区画)を定義する。

    Returns:
        Dict[str, tk.Widget]: {"スロット名": フレームWidget} の辞書

    Example:
        # ヘッダー
        self.header_frame = tk.Frame(self, height=60, bg='navy')
        self.header_frame.pack(fill=tk.X)

        # メインコンテンツ
        self.main_frame = tk.Frame(self)
        self.main_frame.pack(fill=tk.BOTH, expand=True)

        # フッター
        self.footer_frame = tk.Frame(self, height=30, bg='gray')
        self.footer_frame.pack(fill=tk.X)

        return {
            "header": self.header_frame,
            "main": self.main_frame,
            "footer": self.footer_frame
        }
    """
    pass

switch_slot_content

switch_slot_content(slot_name: str, cls: ComponentType, kwargs: dict = None) -> None

指定スロットのコンテンツを切り替える。

Parameters:

Name Type Description Default
slot_name str

スロット名

required
cls ComponentType

コンポーネントクラス(Container/Presentational両対応)

required
kwargs dict

コンポーネントに渡す引数

None
Source code in src/pubsubtk/ui/base/template_base.py
def switch_slot_content(
    self, slot_name: str, cls: ComponentType, kwargs: dict = None
) -> None:
    """
    指定スロットのコンテンツを切り替える。

    Args:
        slot_name: スロット名
        cls: コンポーネントクラス(Container/Presentational両対応)
        kwargs: コンポーネントに渡す引数
    """
    if slot_name not in self._slots:
        raise ValueError(f"Unknown slot: {slot_name}")

    # 既存のコンテンツを破棄
    if slot_name in self._slot_contents:
        self._slot_contents[slot_name].destroy()

    # 新しいコンテンツを作成
    parent_frame = self._slots[slot_name]
    content = self._create_component_for_slot(cls, parent_frame, kwargs)
    content.pack(fill=tk.BOTH, expand=True)

    self._slot_contents[slot_name] = content

get_slots

get_slots() -> Dict[str, Widget]

定義されているスロットの辞書を返す

Source code in src/pubsubtk/ui/base/template_base.py
def get_slots(self) -> Dict[str, tk.Widget]:
    """定義されているスロットの辞書を返す"""
    return self._slots.copy()

get_slot_content

get_slot_content(slot_name: str) -> Widget | None

指定スロットの現在のコンテンツを返す

Source code in src/pubsubtk/ui/base/template_base.py
def get_slot_content(self, slot_name: str) -> tk.Widget | None:
    """指定スロットの現在のコンテンツを返す"""
    return self._slot_contents.get(slot_name)

has_slot

has_slot(slot_name: str) -> bool

指定した名前のスロットが存在するかチェック

Source code in src/pubsubtk/ui/base/template_base.py
def has_slot(self, slot_name: str) -> bool:
    """指定した名前のスロットが存在するかチェック"""
    return slot_name in self._slots

clear_slot

clear_slot(slot_name: str) -> None

指定スロットのコンテンツをクリアする

Source code in src/pubsubtk/ui/base/template_base.py
def clear_slot(self, slot_name: str) -> None:
    """指定スロットのコンテンツをクリアする"""
    if slot_name in self._slot_contents:
        self._slot_contents[slot_name].destroy()
        del self._slot_contents[slot_name]

clear_all_slots

clear_all_slots() -> None

すべてのスロットのコンテンツをクリアする

Source code in src/pubsubtk/ui/base/template_base.py
def clear_all_slots(self) -> None:
    """すべてのスロットのコンテンツをクリアする"""
    for slot_name in list(self._slot_contents.keys()):
        self.clear_slot(slot_name)

TemplateComponentTk

TemplateComponentTk(parent: Widget, store: Store[TState], *args, **kwargs)

Bases: TemplateMixin[TState], Frame, Generic[TState]

標準tk.Frameベースのテンプレートコンポーネント。

tk.Frame ベースのテンプレートを初期化する。

Source code in src/pubsubtk/ui/base/template_base.py
def __init__(self, parent: tk.Widget, store: Store[TState], *args, **kwargs):
    """tk.Frame ベースのテンプレートを初期化する。"""

    tk.Frame.__init__(self, master=parent)
    TemplateMixin.__init__(self, store=store, *args, **kwargs)

TemplateComponentTtk

TemplateComponentTtk(parent: Widget, store: Store[TState], *args, **kwargs)

Bases: TemplateMixin[TState], Frame, Generic[TState]

テーマ対応ttk.Frameベースのテンプレートコンポーネント。

ttk.Frame ベースのテンプレートを初期化する。

Source code in src/pubsubtk/ui/base/template_base.py
def __init__(self, parent: tk.Widget, store: Store[TState], *args, **kwargs):
    """ttk.Frame ベースのテンプレートを初期化する。"""

    ttk.Frame.__init__(self, master=parent)
    TemplateMixin.__init__(self, store=store, *args, **kwargs)