Note that there are some explanatory texts on larger screens.

plurals
  1. POwxPython: Can a wx.PyControl contain a wx.Sizer?
    primarykey
    data
    text
    <p>Can a <code>wx.PyControl</code> contain a <code>wx.Sizer</code>?</p> <p>Note that what I am ultimately trying to do here (spinner with float values) is already answered in another question. I am particularly interested in layouting widgets within a <code>wx.PyControl</code>, a skill which might prove useful if I come across a need to make my own custom widgets. I already read through <a href="http://wiki.wxpython.org/CreatingCustomControls" rel="nofollow noreferrer">CreatingCustomControls</a>, but it didn't use sizers within the <code>wx.PyControl</code> subclass.</p> <p>Using my code below, my <code>CustomWidget</code> just doesn't look right. I'm not yet doing a <code>DoGetBestSize</code> because I think that applies to a <code>wx.Sizer</code> acting <strong>on</strong> a widget. I am actually having a <code>wx.Sizer</code> doing its thing <strong>inside</strong> a <code>CustomWidget</code>.</p> <p>Here is my code (without the event bindings between sub-widgets):<br> <strong>EDIT:</strong> Here is my corrected class code, thanks to <a href="https://stackoverflow.com/questions/3271639/wxpython-can-a-wx-pycontrol-contain-a-wx-sizer/3272377#3272377">Steven Sproat</a>:</p> <pre><code>import wx class CustomWidget(wx.PyControl): def __init__(self, parent): wx.PyControl.__init__(self, parent=parent, style=wx.NO_BORDER) # Style added. text = wx.TextCtrl(parent=self) spin = wx.SpinButton(parent=self, style=wx.SP_VERTICAL) sizer = wx.GridBagSizer() self.layout(text, spin, sizer) self.OnInit(text, sizer) def OnInit(self, text, sizer): text.SetValue(u"0.000") def layout(self, text, spin, sizer): self.SetSizer(sizer) sizer.Add(text, pos=(0, 0), flag=wx.ALIGN_CENTER) sizer.Add(spin, pos=(0, 1), flag=wx.ALIGN_CENTER) self.Fit() self.Layout() # This is what I lacked. I needed to call .Layout() self.CenterOnParent() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload