AgentSkillsCN

Create GUI Component

掌握创建解耦 wxPython 组件(面板、对话框)的标准规范。

SKILL.md
--- frontmatter
name: Create GUI Component
description: Standards for creating decoupled wxPython components (Panels, Dialogs).

🖥️ GUI Component Creation Skill

Context

wxPython code must be clean, decoupled from logic, and use sizers for layout.

1. Standard Panel Structure

Always inherit from wx.Panel. never put business logic directly here. Use EventBus to talk to services.

python
import wx
from src.core.events import EventBus, EventType

class MyFeaturePanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self._init_ui()
        self._bind_events()

    def _init_ui(self):
        # Master Sizer
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        # Components
        self.btn_action = wx.Button(self, label="Do Item")
        
        # Layout
        main_sizer.Add(self.btn_action, 0, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(main_sizer)

    def _bind_events(self):
        self.btn_action.Bind(wx.EVT_BUTTON, self._on_action)

    def _on_action(self, event):
        # Delegate to Event Bus - DO NOT call Service directly
        EventBus().publish(EventType.USER_ACTION, {"action": "do_item"})

2. Dialog Standard

Use wx.Dialog with standard buttons.

python
class MyDialog(wx.Dialog):
    def __init__(self, parent, title="My Dialog"):
        super().__init__(parent, title=title)
        self._init_ui()

    def _init_ui(self):
        sizer = wx.BoxSizer(wx.VERTICAL)
        
        # Content
        self.text_input = wx.TextCtrl(self)
        sizer.Add(self.text_input, 0, wx.EXPAND | wx.ALL, 10)

        # Buttons
        btns = self.CreateButtonSizer(wx.OK | wx.CANCEL)
        sizer.Add(btns, 0, wx.EXPAND | wx.ALL, 10)
        
        self.SetSizerAndFit(sizer)

3. Checklist

  • No logic in _on_event methods? (Publish events instead).
  • Used BoxSizer or GridBagSizer? (No absolute positioning).
  • All user-visible strings in English?