首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wxPython ScrolledWindow太小

wxPython ScrolledWindow太小
EN

Stack Overflow用户
提问于 2012-06-01 02:12:06
回答 1查看 1.6K关注 0票数 0

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

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

代码语言:javascript
复制
# 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()

我想不出哪个面板需要调整大小。我尝试过的一些事情,都无济于事:

代码语言:javascript
复制
# 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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-01 04:27:40

我对你的代码做了一些修改,让它正常工作:

代码语言:javascript
复制
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“:

代码语言:javascript
复制
lineBoxSizer.Add(self.scroll, 1, wx.EXPAND)    
boxSizer.Add(lineBoxSizer, 1, wx.EXPAND)

我改变了启动程序的方式,因为在这种情况下,将一个框架放入另一个框架中有点愚蠢。而且PySimpleApp也被弃用了,所以我也改变了它。我几乎从来没有找到过"Fit()“方法的好用法,所以我把它去掉了,因为它对初始GUI的挤压太大了。

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10838956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档