ScrolledPanel with vertical scrollbar only and WrapSizer

后端 未结 2 1821
梦谈多话
梦谈多话 2021-01-14 03:08

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

相关标签:
2条回答
  • 2021-01-14 03:33

    Solution:

    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()
    
    0 讨论(0)
  • 2021-01-14 03:39

    It looks like you just forgot to include

    self.SetSizer(sizer)
    

    Since the WrapSizer takes the whole frame, I think that will work. Also, instead of SetupScrolling, you can use

    self.SetScrollRate(horiz, vert)
    

    to specify the increment (in pixels, i think) of the scroll, and that should work.

    I can't test it here right now though, and WrapSizers are a little weird - they sometimes have trouble figuring out their proper size. You may need to wrap it in a BoxSizer going the other direction.

    0 讨论(0)
提交回复
热议问题