Hello,
I’m trying to create my own SpinCtrl using a TextCtrl and a SpinButton. I’ve managed to succesfully bind their events and everything, but my component still does not behave as expected when regards to expanding. The relevant parts of my code (ignoring event handling and extra logic)
class CustomSpinCtrl(wx.Panel):
def init(self, *args, **kwargs):
super(wx.Panel, self).init(*args, **kwargs)
self._text_input = wx.TextCtrl(self, -1)
height = self._text_input.GetSize().height
width = self._text_input.GetSize().width
Place spin button at right just like normal wx.SpinCtrl
self._spin_button = wx.SpinButton(self, -1, (width, 0), (height * 5 / 6, height),
wx.SP_VERTICAL)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self._text_input, 1, wx.EXPAND | wx.ALIGN_CENTRE_VERTICAL, 0)
self.SetSizer(self.sizer)
self.Bind(wx.EVT_TEXT, self.text_entered, self._text_input)
self._text_input.Bind(wx.EVT_KILL_FOCUS, self._on_kill_focus)
self._text_input.Bind(wx.EVT_SET_FOCUS, self._on_set_focus)
self._text_input.Bind(wx.EVT_KEY_DOWN, self._on_key_down)
self.Bind(wx.EVT_SPIN, self.OnSpin, self._spin_button)
Now how I use it, in the code below ‘self.parent’ is a wx.Panel from a hierarchy of panels:
self.entry = CustomSpinCtrl(self.parent, -1)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.entry, 1, wx.EXPAND | wx.ALIGN_CENTRE_VERTICAL, 0)
self.parent.SetSizer(self.sizer)
Now if I replace in the lines above ‘self.entry = CustomSpinCtrl(self.parent, -1)’ with for example ‘self.entry = wx.SpinCtrl(self.parent, -1)’ or ‘self.entry = wx.TextCtrl(self.parent, -1)’ then it behaves as expected and expands to fill the width of the parent panel, but I can’t figure out why this is not the case for my CustomSpinCtrl. I’ve also atached a print with the relevant part, there you can see a wx.TextCtrl and a CustomSpinCtrl added using the exact same logic, and how they scale differently.