expand panel and textctrl

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.

layout_issue.png

Hi,

A good problem to use the WIT for debugging it.

See attached, to make your control to behave like the wx one I had to add it to the sizer and then it looks like it is working.

I found this by creating this runnable sample and using the WIT. Run the sample and press ctrl-alt-i and find your custom control and use the Highlight toolbar button, then uncomment the line to add the spin button to the sizer and run it again.

Werner

customspin.py (1.2 KB)

Hello,

Thank you for the input, but this still does not work for me. I’ve atached print, as you can see the custom component does not expand to frame. I user WIT like you said but can not seem to find out what the issue is. From what I can see with WIT the top level panel is indeed expanded but the TextCtrl won’t for some reason.

sâmbătă, 30 martie 2013, 14:57:03 UTC+2, Bogdan Valentin Neacsa a scris:

···

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.

Hi Bogdan,

Hello,

Thank you for the input, but this still does not work for me. I've atached print, as you can see the custom component does not expand to frame. I user WIT like you said but can not seem to find out what the issue is. From what I can see with WIT the top level panel is indeed expanded but the TextCtrl won't for some reason.

In the sample I sent that should work - at least it does for me.

I use sized_controls and this line does it:

cSp.SetSizerProps(expand=True)

which would be:

sizer.Add(your custom control, flag=wx.EXPAND)

And possibly he parent of your custom control needs to expand too.

If you don't find the issue post a runnable sample and I have a look at it.

Werner

···

On 30/03/2013 16:22, Bogdan Valentin Neacsa wrote:

Hello,

Again thank you for your response. I’m running your exact sample when getting the screen I atached. I would think this is some kind of issue with my envornment but I’ve tested it both on a Windows, wxPython 2.8.11.0 (msw-unicode) and on Ubuntu wxPython 2.8.11.1 (gtk-unicode) and I get the same result.

sâmbătă, 30 martie 2013, 14:57:03 UTC+2, Bogdan Valentin Neacsa a scris:

···

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.

With my sample and line 21 uncommented I get this:
This is on Windows 7, Python 2.7, wxPython 2.9.5, if I run it on
2.8.12 then I see your problem.
Upgrade to 2.9.5 if you can - many enhancements and fixes compared
to 2.8.11.
If you can’t then maybe the attached will help you get going.
Instead of wx.Panel it uses wx.PyPanel which allows to override
DoGetBestSize and this one
works for me on 2.8.12 but you need to fine tune the sizing of your
custom control.
It doesn’t quit size right and I am not sure why - not that strong
in doing custom controls. You might find some hints/answers in:
Werner

customspin.py (1.38 KB)

···

Hi,

  On 30/03/2013 17:54, Bogdan Valentin Neacsa wrote:

Hello,

    Again thank you for your response. I'm running your exact

sample when getting the screen I atached. I would think this is
some kind of issue with my envornment but I’ve tested it both on
a Windows, wxPython 2.8.11.0 (msw-unicode) and on Ubuntu
wxPython 2.8.11.1 (gtk-unicode) and I get the same result.

http://wiki.wxpython.org/CreatingCustomControls