Standards for creating decoupled wxPython components (Panels, Dialogs).
wxPython code must be clean, decoupled from logic, and use sizers for layout.
Always inherit from wx.Panel. never put business logic directly here. Use EventBus to talk to services.
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"})
Use wx.Dialog with standard buttons.
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)
_on_event methods? (Publish events instead).BoxSizer or GridBagSizer? (No absolute positioning).