0

我有一个面板来控制 wxPython 框架中 matplotlib 图的编辑。wxPython 安装最近从 2.6.4.0 更新到 2.8.12.1,它破坏了一些东西,即滚动面板不再填充块,而是保持在最小尺寸。我现在才从一年前捡起这个,所以我有点生疏了。任何帮助将非常感激!

下面是可以独立运行并显示问题的代码的精简版本。ScrolledWindow 应该扩大到 400 像素。当我运行它时它会self.scroll.GetSize()返回(292, 257),但它显然没有以那个大小显示。

# testing scroll panel for PlotEditFrame

import wx

# spoof the necessary matplotlib objects
class FakePlot:
    def __init__(self):
        self.figure = FakeFigure()
    def get_figure(self):
        return self.figure
class FakeFigure:
    def __init__(self):
        self.axes = [FakeAxis() for i in range(0,2)]
class FakeAxis:
    def __init__(self):
        self.lines = [FakeLine(i) for i in range(0, 4)]
class FakeLine:
    def __init__(self,i):
        self.label = "line #%s"%i
    def get_label(self):
        return self.label

class PlotEditFrame(wx.Frame):
    """
    This class holds the frame for plot editing tools
    """

    def __init__(self, parent, plot):
        """Constructor for PlotEditFrame"""
        wx.Frame.__init__(self, parent, -1, "Edit Plot")
        self.parent = parent
        self.plot = plot
        self.figure = plot.get_figure()
        self.advanced_options = None
        self.scroll = wx.ScrolledWindow(self, -1)
        self.InitControls()

    def InitControls(self):
        """Create labels and controls based on the figure's attributes"""

        # Get current axes labels
        self.lineCtrls = [( wx.StaticText(self.scroll, -1, "Column:"),
                            wx.StaticText(self.scroll, -1, "Color:"),
                            wx.StaticText(self.scroll, -1, ""))]

        for axis in self.figure.axes:
            for line in axis.lines:
                color = wx.Colour(255,0,0,0)
                lineTxt = wx.TextCtrl(self.scroll, -1, line.get_label(), size=(175,-1))
                lineColor = wx.TextCtrl(self.scroll, -1, "#%02x%02x%02x"%color.Get())
                lineBtn = wx.Button(self.scroll, -1, size=(25,25))
                lineBtn.SetBackgroundColour(color)
                self.lineCtrls.append((lineTxt, lineColor, lineBtn))

        # Place controls
        boxSizer = wx.BoxSizer(wx.VERTICAL)

        lineBox = wx.StaticBox(self, -1, "Lines")
        lineBoxSizer = wx.StaticBoxSizer(lineBox, wx.VERTICAL)
        lineSizer = wx.FlexGridSizer(rows=len(self.lineCtrls)+1, cols=4, vgap=3, hgap=3)
        for ctrls in self.lineCtrls:
            lineSizer.AddMany([(ctrls[0], 0, wx.ALIGN_LEFT | wx.EXPAND),
                               (ctrls[1], 0, wx.ALIGN_LEFT),
                               (ctrls[2], 0, wx.ALIGN_CENTER| wx.FIXED_MINSIZE),
                               ((3,3),    0, wx.ALIGN_CENTER)])
        lineSizer.AddGrowableCol(0)

        # Set size
        self.scroll.SetSizer(lineSizer)
        width = self.scroll.GetBestSize().width
        height = self.scroll.GetBestSize().height
        if height > 400:
            height = 400
            width = width + 25 # button size
        self.scroll.SetSize((width, height))
        self.scroll.SetScrollbars(0, 1, 1,1)
        print "set scrollbars at %s x %s"%(width, height)

        lineBoxSizer.Add(self.scroll, 0, wx.EXPAND)

        boxSizer.AddMany([ (lineBoxSizer, 0, wx.EXPAND) ])

        self.SetSizer(boxSizer)
        self.SetAutoLayout(1)
        self.Fit()

        height = self.GetSize().GetHeight()
        self.SetSizeHints(minH=height, maxH=height,
                                minW=width, maxW=width*5)

if __name__ == '__main__':
    app = wx.PySimpleApp(0)
    parent = wx.Frame(None, wx.ID_ANY, 'test', size=(300,300))
    plot = FakePlot()
    panel = PlotEditFrame(parent, plot)
    panel.Show()
    app.MainLoop()

我不知道哪个面板需要调整大小。我尝试了一些事情,但无济于事:

# These have no visible effect
boxSizer.SetMinSize((width, height))
self.scroll.SetVirtualSize((width, height))
lineBoxSizer.Fit(self.scroll)
lineBoxSizer.SetVirtualSizeHints(self.scroll)

# This makes the window the right size, but not the scroll panel
lineBoxSizer.SetMinSize((width, height))
4

1 回答 1

1

我对您的代码进行了一些编辑以使其正常工作:

import wx

# spoof the necessary matplotlib objects
class FakePlot:
    def __init__(self):
        self.figure = FakeFigure()
    def get_figure(self):
        return self.figure
class FakeFigure:
    def __init__(self):
        self.axes = [FakeAxis() for i in range(0,2)]
class FakeAxis:
    def __init__(self):
        self.lines = [FakeLine(i) for i in range(0, 4)]
class FakeLine:
    def __init__(self,i):
        self.label = "line #%s"%i
    def get_label(self):
        return self.label

class PlotEditFrame(wx.Frame):
    """
    This class holds the frame for plot editing tools
    """

    def __init__(self, parent, plot, size):
        """Constructor for PlotEditFrame"""
        wx.Frame.__init__(self, parent, -1, "Edit Plot", size=size)
        self.parent = parent
        self.plot = plot
        self.figure = plot.get_figure()
        self.advanced_options = None
        self.scroll = wx.ScrolledWindow(self, -1)
        self.InitControls()

    def InitControls(self):
        """Create labels and controls based on the figure's attributes"""

        # Get current axes labels
        self.lineCtrls = [( wx.StaticText(self.scroll, -1, "Column:"),
                            wx.StaticText(self.scroll, -1, "Color:"),
                            wx.StaticText(self.scroll, -1, ""))]

        for axis in self.figure.axes:
            for line in axis.lines:
                color = wx.Colour(255,0,0,0)
                lineTxt = wx.TextCtrl(self.scroll, -1, line.get_label(), size=(175,-1))
                lineColor = wx.TextCtrl(self.scroll, -1, "#%02x%02x%02x"%color.Get())
                lineBtn = wx.Button(self.scroll, -1, size=(25,25))
                lineBtn.SetBackgroundColour(color)
                self.lineCtrls.append((lineTxt, lineColor, lineBtn))

        # Place controls
        boxSizer = wx.BoxSizer(wx.VERTICAL)

        lineBox = wx.StaticBox(self, -1, "Lines")
        lineBoxSizer = wx.StaticBoxSizer(lineBox, wx.VERTICAL)
        lineSizer = wx.FlexGridSizer(rows=len(self.lineCtrls)+1, cols=4, vgap=3, hgap=3)
        for ctrls in self.lineCtrls:
            lineSizer.AddMany([(ctrls[0], 0, wx.ALIGN_LEFT | wx.EXPAND),
                               (ctrls[1], 0, wx.ALIGN_LEFT),
                               (ctrls[2], 0, wx.ALIGN_CENTER| wx.FIXED_MINSIZE),
                               ((3,3),    0, wx.ALIGN_CENTER)])
        lineSizer.AddGrowableCol(0)

        # Set size
        self.scroll.SetSizer(lineSizer)
        width = self.scroll.GetBestSize().width
        height = self.scroll.GetBestSize().height
        if height > 400:
            height = 400
            width = width + 25 # button size
        self.scroll.SetSize((width, height))
        self.scroll.SetScrollbars(0, 1, 1,1)
        print "set scrollbars at %s x %s"%(width, height)

        lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)

        boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)

        self.SetSizer(boxSizer)
        self.SetAutoLayout(1)
        #self.Fit()

        height = self.GetSize().GetHeight()
        self.SetSizeHints(minH=height, maxH=height,
                                minW=width, maxW=width*5)

if __name__ == '__main__':
    app = wx.App(False)
    plot = FakePlot()
    frame = PlotEditFrame(None, plot, size=(300,300))
    frame.Show()
    app.MainLoop()

主要是将以下两行的比例设置为“1”:

lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)    
boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)

我改变了你启动程序的方式,因为在这种情况下将一个框架放在另一个框架内有点傻。PySimpleApp 也被弃用了,所以我也改变了它。我几乎从来没有找到“Fit()”方法的好用处,所以我把它去掉了,因为它过多地压缩了初始 GUI。

希望有帮助!

于 2012-05-31T20:27:39.887 回答