0

これは次の質問の続きです:
wxPython: wx.PyControl に wx.Sizer を含めることはできますか?

ここでの主なトピックは、 a のwx.Sizer中で aを使用することwx.PyControlです。子ウィジェットFit()の周りで問題が発生しました。CustomWidgetこの問題は、Layout()afterを呼び出すことで解決されましたFit()

ただし、私が経験した限り、ソリューションCustomWidgetは が の直接の子である場合にのみ機能しwx.Frameます。の子になると壊れるwx.Panel

編集:以下のコードを使用すると、CustomWidgetその子に合わせてサイズが正しく変更されません。CustomWidgetこれは、 (のサブクラスとして)が;wx.PyControlの子である場合にのみ発生することがわかりました。wx.Panelそれ以外の場合、それが a の直接の子である場合、それはwx.Frame完全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)

    # 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)仕事をします。.SetSizer(sizer)a 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 に答える