本文介绍了wxPython:当它是wx.Panel的子级时,wx.PyControl布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是该问题的延续:

此处的主要主题是使用 wx .sizer 放在 wx.PyControl 中。我在 Fit() CustomWidget 设置在其子小部件周围时遇到问题。通过在 Fit()之后调用 Layout()解决了该问题。

The main topic here is using a wx.Sizer inside a wx.PyControl. I had problems Fit()ting my CustomWidget around its child widgets. That problem was solved by calling Layout() after Fit().

但是,据我所知,该解决方案仅在 CustomWidget 是<$ c的直接子项时有效$ c> wx.Frame 。当它成为 wx.Panel 的子代时,它会崩溃。

However, as far as I have experienced, the solution only works when the CustomWidget is a direct child of a wx.Frame. It breaks down when it becomes a child of a wx.Panel.

编辑:使用下面的代码, CustomWidget 的大小无法正确调整以适合其子级。我观察到只有当 CustomWidget (作为 wx.PyControl 的子类)是 wx.Panel ;否则,如果它是 wx.Frame 的直接子代,则它 Fit()是完美的。

Using the code below, the CustomWidget doesn't resize correctly to fit its children. I observed that this only happens when the CustomWidget (as a subclass of wx.PyControl) is a child of a wx.Panel; otherwise, if it is a direct child of a wx.Frame, it Fit()s perfectly.

这是代码:

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()


推荐答案

.SetSizerAndFit(sizer)完成工作。我不确定为什么 .SetSizer(sizer)而不是 .Fit()为何不起作用。有想法吗?

.SetSizerAndFit(sizer) does the job. I'm not sure why a .SetSizer(sizer) then a .Fit() won't work. Any ideas?

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()

这篇关于wxPython:当它是wx.Panel的子级时,wx.PyControl布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:01