wxPython滚动窗口太sm

2024-10-01 22:27:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

下面是代码的精简版本,可以独立运行并显示问题。滚动窗口应该扩展到400px。当我运行它时,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()

我不知道什么面板需要调整大小。我做了一些尝试,但没有成功:

^{pr2}$

Tags: inselfforsizeplotinitdefwidth
1条回答
网友
1楼 · 发布于 2024-10-01 22:27:23

我编辑了你的代码让它正常工作:

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”:

^{pr2}$

我改变了你启动程序的方式,因为把一个框架放在另一个框架里有点傻。而且PySimpleApp也不推荐使用,所以我也修改了它。我几乎从来没有发现“Fit()”方法有什么好的用途,所以我把它去掉了,因为它太挤压了最初的GUI。在

希望有帮助!在

相关问题 更多 >

    热门问题