0

这是这个问题的延续:
wxPython: Can a wx.PyControl contains a wx.Sizer?

这里的主要主题是使用 a wx.Sizerinside a wx.PyControlFit()我在CustomWidget围绕它的子小部件时遇到了问题。Layout()通过调用after解决了这个问题Fit()

但是,据我所知,该解决方案仅在CustomWidget是 a 的直接子代时才有效wx.Frame。当它成为 a 的孩子时,它就会崩溃wx.Panel

编辑:使用下面的代码,CustomWidget不会正确调整大小以适合其子级。我观察到只有当CustomWidget(作为 的子类wx.PyControl)是 a 的子类时才会发生这种情况wx.Panel;否则,如果它是 a 的直接子代,则wx.FrameFit()是完美的。

这是代码:

import wx

class Frame(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, parent=None)
    panel = Panel(parent=self)
    custom = CustomWidget(parent=panel)
    self.Show()

class Panel(wx.Panel):
  def __init__(self, parent):
    wx.Panel.__init__(self, parent=parent)
    self.SetSize(parent.GetClientSize())

class CustomWidget(wx.PyControl):
  def __init__(self, parent):
    wx.PyControl.__init__(self, parent=parent)

    # Create the sizer and make it work for the CustomWidget        
    sizer = wx.GridBagSizer()
    self.SetSizer(sizer)

    # Create the CustomWidget's children
    text = wx.TextCtrl(parent=self)
    spin = wx.SpinButton(parent=self, style=wx.SP_VERTICAL)

    # Add the children to the sizer        
    sizer.Add(text, pos=(0, 0), flag=wx.ALIGN_CENTER)
    sizer.Add(spin, pos=(0, 1), flag=wx.ALIGN_CENTER)

    # Make sure that CustomWidget will auto-Layout() upon resize
    self.Bind(wx.EVT_SIZE, self.OnSize)
    self.Fit()

  def OnSize(self, event):
    self.Layout()

app = wx.App(False)
frame = Frame()
app.MainLoop()
4

1 回答 1

1

.SetSizerAndFit(sizer)做这项工作。我不确定为什么 a .SetSizer(sizer)then a.Fit()不起作用。有任何想法吗?

import wx

class Frame(wx.Frame):
  def __init__(self):
    wx.Frame.__init__(self, parent=None)
    panel = Panel(parent=self)
    custom = CustomWidget(parent=panel)
    self.Show()

class Panel(wx.Panel):
  def __init__(self, parent):
    wx.Panel.__init__(self, parent=parent)
    self.SetSize(parent.GetClientSize())

class CustomWidget(wx.PyControl):
  def __init__(self, parent):
    wx.PyControl.__init__(self, parent=parent)

    # Create the sizer and make it work for the CustomWidget        
    sizer = wx.GridBagSizer()
    self.SetSizer(sizer)

    # Create the CustomWidget's children
    text = wx.TextCtrl(parent=self)
    spin = wx.SpinButton(parent=self, style=wx.SP_VERTICAL)

    # Add the children to the sizer        
    sizer.Add(text, pos=(0, 0), flag=wx.ALIGN_CENTER)
    sizer.Add(spin, pos=(0, 1), flag=wx.ALIGN_CENTER)

    # Set sizer and fit, then layout
    self.SetSizerAndFit(sizer)
    self.Layout()

  # ------------------------------------------------------------
  #  # Make sure that CustomWidget will auto-Layout() upon resize
  #  self.Bind(wx.EVT_SIZE, self.OnSize)
  #  self.Fit()
  #  
  #def OnSize(self, event):
  #  self.Layout()
  # ------------------------------------------------------------    

app = wx.App(False)
frame = Frame()
app.MainLoop()
于 2010-07-23T15:19:21.383 回答