本文介绍了展开时将 sizer 中的更多大小分配给 wx.CollapsiblePane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个垂直的 BoxSizer 中有几个 CollapsiblePanes.我希望能够在不让它们相互碰撞的情况下扩展和折叠它们.我在 Windows 7 上运行 wxPython 2.8.10.1.

I have several CollapsiblePanes in a vertical BoxSizer. I would like to be able to expand and collapse them without having them run into each other. I am running wxPython 2.8.10.1 on Windows 7.

演示问题的可运行示例应用程序如下.

Runnable sample application demonstrating the problem is below.

import wx

class SampleCollapsiblePane(wx.CollapsiblePane):
    def __init__(self, *args, **kwargs):
        wx.CollapsiblePane.__init__(self,*args,**kwargs)
        sizer = wx.BoxSizer(wx.VERTICAL)
        for x in range(5):
            sizer.Add(wx.Button(self.GetPane(), label = str(x)))
        self.GetPane().SetSizer(sizer)


class Main_Frame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.main_panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        for x in range(5):
            sizer.Add(SampleCollapsiblePane(self.main_panel, label = str(x)), 1)
        self.main_panel.SetSizer(sizer)


class SampleApp(wx.App):
    def OnInit(self):
        frame = Main_Frame(None, title = "Sample App")
        frame.Show(True)
        frame.Centre()
        return True

def main():
    app = SampleApp(0)
    app.MainLoop()

if __name__ == "__main__":
    main()

推荐答案

文档明确指出在将可折叠窗格添加到 sizer 时应使用 ratio=0.

The documentation explicitly states that you should use proportion=0 when adding collapsible panes to a sizer.

http://docs.wxwidgets.org/stable/wx_wxcollapsiblepane.html

因此,首先,将此行末尾的 1 更改为 0:

So, first, change the 1 at the end of this line to a 0:

sizer.Add(SampleCollapsiblePane(self.main_panel, label = str(x)), 1)

接下来,将此添加到您的 SampleCollapsiblePane 以强制父框架在窗格折叠或展开时重新布局:

Next, add this to your SampleCollapsiblePane to force the parent frame to re-layout when a pane is collapsed or expanded:

def __init__(...):
    ...
    self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.on_change)
def on_change(self, event):
    self.GetParent().Layout()

可能有更好的方法,但这就是我目前的工作.我很擅长 wxPython,但之前没有使用过 CollapsiblePanes.

There might be a better way, but this is what I've got working at the moment. I'm good with wxPython but haven't used CollapsiblePanes before.

这篇关于展开时将 sizer 中的更多大小分配给 wx.CollapsiblePane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:09