コンテンツにスキップ

APIリファレンス

PubSubTkライブラリの詳細なAPI仕様や活用パターンをまとめています。
サイドバーから各クラス・モジュールの詳細ページへ移動できます。


モジュール構成

PubSubTkは主に次のモジュール群で構成されています:

Core

Application Framework

UI Components

Storybook

  • pubsubtk.storybook
  • StorybookApplication … Storybookアプリケーション
  • @story … ストーリー定義デコレータ
  • StoryContext … ストーリー実行コンテキスト

  • pubsubtk.storybook.knobs

  • KnobSpec … Knobの仕様定義
  • KnobValue … Knob値オブジェクト

Utilities


クイックナビゲーション

よく使うクラス

クラス 用途 リンク
TkApplication アプリケーション本体 pubsubtk.app
ContainerComponentTk 状態連携UI pubsubtk.ui
PresentationalComponentTk 純粋表示UI pubsubtk.ui
ProcessorBase ビジネスロジック pubsubtk.processor
Store 状態管理 pubsubtk.store
StorybookApplication コンポーネント開発環境 pubsubtk.storybook
@story ストーリー定義 pubsubtk.storybook.core

主なメソッド

メソッド 説明 代表的な用途
pub_update_state() 状態を更新 Container, Processor
pub_switch_container() 画面切り替え Container, Processor
sub_state_changed() 状態変更監視 Container
setup_subscriptions() 購読設定 Container, Processor
refresh_from_state() UI更新 Container
ctx.knob() Storybook動的コントロール Story定義内

よくある使用パターン

状態管理の基本

from pydantic import BaseModel
from pubsubtk import TkApplication, ContainerComponentTk

class AppState(BaseModel):
    counter: int = 0

class MainContainer(ContainerComponentTk[AppState]):
    def setup_ui(self): ...
    def setup_subscriptions(self):
        self.sub_state_changed(self.store.state.counter, self.on_counter_changed)
    def refresh_from_state(self): ...

app = TkApplication(AppState)
app.switch_container(MainContainer)
app.run()

Processorの活用

from pubsubtk import ProcessorBase

class MyProcessor(ProcessorBase[AppState]):
    def setup_subscriptions(self):
        self.subscribe("custom.event", self.handle_event)
    def handle_event(self):
        self.pub_update_state(self.store.state.counter, 42)

# アプリ起動時にProcessor登録
app.pub_register_processor(MyProcessor)

テンプレートの使い方

from pubsubtk import TemplateComponentTk

class AppTemplate(TemplateComponentTk[AppState]):
    def define_slots(self):
        return {
            "header": self.header_frame,
            "main": self.main_frame,
        }

app.set_template(AppTemplate)
app.pub_switch_slot("main", MainContainer)

Storybookでのコンポーネント開発

from pubsubtk.storybook import story, StorybookApplication

@story("Button.Primary")
def primary_button(ctx):
    text = ctx.knob("text", str, "Click me!")
    size = ctx.knob("size", int, 12, range_=(8, 24))

    import tkinter as tk
    btn = tk.Button(ctx.parent, text=text.value, font=("", size.value))
    btn.pack()
    return btn

app = StorybookApplication()
app.run()

型安全・IDE支援

  • StateProxyで型安全な状態更新・補完・定義ジャンプが可能
  • ジェネリクスにより、各Store/Container/Processorで型が明示でき、ミス防止&開発効率UP

デバッグ・ユーティリティ

  • enable_pubsub_debug_logging() でPubSubのメッセージ流れを確認可能
  • 状態のスナップショットや復元もPydantic標準で手軽

命名規則

  • クラス名: PascalCase(例: MainContainer
  • メソッド名: snake_case(例: setup_ui
  • トピック名: UPPER_CASE(例: USER_LOGIN
  • 状態フィールド: snake_case(例: user_name

関連リンク


各APIの詳細は左の「API詳細」ナビゲーションからご覧ください。