本文介绍了仅带有垂直滚动条和 WrapSizer 的 ScrolledPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WrapSizer 来实现像这样的自动布局(作为缩略图库)(见左侧的截图):

I uses a WrapSizer in order to have an automatic layout (as thumbnail gallery) like this (see screenshot on the left) :

如果有两个多元素,我希望在面板上添加一个(仅限垂直)-ScrollBar(见右截图).如何使用 WrapSizer 向面板添加这样的垂直滚动条?

I would like that if there are two many elements, a (vertical only)-ScrollBar is added on the panel (see right screenshot). How to add such a vertical scrollbar to a panel using a WrapSizer?

我尝试混合使用 WrapSizerScrolledPanel,但我无法获得所需的布局.

I tried by mixing WrapSizer and ScrolledPanel, but I cannot get the desired layout.

class MyPanel(scrolled.ScrolledPanel):
    def __init__(self, parent):
        scrolled.ScrolledPanel.__init__(self, parent)
        self.SetBackgroundColour('#f8f8f8')
        sizer = wx.WrapSizer()
        self.SetupScrolling()

        # add some widgets btn1, btn2, etc. in the WrapSizer
        sizer.Add(btn1, 0, wx.ALL, 10)
        sizer.Add(btn2, 0, wx.ALL, 10)

推荐答案

解决方案:

将滚动面板虚拟大小的宽度重置为可显示大小.

reset the width of the scroll panel virtual size to the displayable size.

import wx
import wx.lib.scrolledpanel as scrolled

class MyPanel(scrolled.ScrolledPanel):
    def __init__(self, parent):
        scrolled.ScrolledPanel.__init__(self, parent, style=wx.VSCROLL)
        self.SetBackgroundColour('#f8f8f8')
        self.sizer = wx.WrapSizer()
        self.SetupScrolling(scroll_x = False)
        self.parent = parent

        self.addButton(self.sizer , 10)
        self.SetSizer(self.sizer )
        self.Bind(wx.EVT_SIZE, self.onSize)

    def onSize(self, evt):
        size = self.GetSize()
        vsize = self.GetVirtualSize()
        self.SetVirtualSize((size[0], vsize[1]))

        evt.Skip()

    def addButton(self, sizer, num):
        for i in range(1, num):
            btn =wx.Button( self, wx.ID_ANY, "btn"+str(i), wx.DefaultPosition, wx.DefaultSize, 0 )
            sizer.Add(btn, 0, wx.ALL, 10)

if __name__=='__main__':
    app = wx.App(redirect=False)
    frame = wx.Frame(None)
    MyPanel(frame)
    frame.Show()
    app.MainLoop()

这篇关于仅带有垂直滚动条和 WrapSizer 的 ScrolledPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:18