🖥️ 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_eventmethods? (Publish events instead). - • Used
BoxSizerorGridBagSizer? (No absolute positioning). - • All user-visible strings in English?