简体   繁体   中英

wxPython ScrolledWindow not working when used in a panel

I am having trouble nesting a ScrolledWindow inside a wx.Panel. If I create a scrolled window on its own it seems to work, however when I create it inside a wx.Panel and add the wx.Panel to the frames sizer it does not. Is there anything that I am missing?

Note:

        #pa = AScrolledWindow(self) <-- if uncommented this works
        pa = ScrolledWindowHolder(self) # However this does not!

import wx


class ScrolledWindowHolder(wx.Panel):
    def __init__(self, parent):
        super(ScrolledWindowHolder, self).__init__(parent=parent)
        mysizer = wx.GridBagSizer()
        self.myscrolledWindow = AScrolledWindow(self)
        mysizer.Add(self.myscrolledWindow, pos=(0, 0), flag=wx.EXPAND)
        self.SetSizerAndFit(mysizer)


class AScrolledWindow(wx.ScrolledWindow):
    def __init__(self, parent):
        super(AScrolledWindow, self).__init__(parent)
        gb = wx.GridBagSizer()
        self.sizer = gb

        self._labels = []
        for y in xrange(1, 30):
            self._labels.append(wx.StaticText(self, -1, "Label #%d" % (y,)))
            gb.Add(self._labels[-1], (y, 1), (1, 1))

        self.SetSizer(self.sizer)
        self.SetScrollRate(5, 5)
        self.EnableScrolling(True, True)


class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Programmatic size change')
        sz = wx.BoxSizer(wx.VERTICAL)
        #pa = AScrolledWindow(self)
        pa = ScrolledWindowHolder(self)
        sz.Add(pa, 1, wx.EXPAND)
        self.SetSizer(sz)


def main():
    wxapp = wx.App()
    fr = TestFrame()
    fr.Show(True)
    wxapp.MainLoop()


if __name__ == '__main__':
    main()

Not sure why but the issue appears to be with the fact that you are using a GridBagSizer with a single widget in ScrolledWindowHolder , which itself contains a GridBagSizer .
If you change ScrolledWindowHolder to use a BoxSizer it works, as expected.

class ScrolledWindowHolder(wx.Panel):
    def __init__(self, parent):
        super(ScrolledWindowHolder, self).__init__(parent=parent)
        mysizer = wx.BoxSizer(wx.HORIZONTAL)
        self.myscrolledWindow = AScrolledWindow(self)
        mysizer.Add(self.myscrolledWindow, 1, wx.EXPAND,0)
        self.SetSizerAndFit(mysizer)

Also, change the value of y to for y in range(1, 60): will demonstrate the scrolled window more effectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM