使用sizer时,如何滚动到ScrolledWindow的底部

时间:2014-06-25 22:00:32

标签: python python-2.7 scroll wxpython

我在wxpython中创建了一个需要可滚动和动态调整大小的文本区域。我借用了some code,除了我似乎无法使用ScrolledWindow.Scroll命令之外,它工作得很好。

以下是代码:

import wx
class AScrolledWindow(wx.ScrolledWindow):
    def __init__(self, parent):
        self.parent = parent
        wx.ScrolledWindow.__init__(self, parent, -1, style=wx.TAB_TRAVERSAL)
        gb = wx.GridBagSizer(vgap=0, hgap=3)
        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)
        fontsz = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT).GetPixelSize()
        self.SetScrollRate(fontsz.x, fontsz.y)
        self.EnableScrolling(True,True)
        self.Scroll(0,100)

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

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

if __name__=='__main__':
    main()

我已经在没有sizer的情况下测试了ScrolledWindow.Scroll命令并且它可以工作,向我建议它不适用于sizer。但是文档中没有提到这种效果。

1 个答案:

答案 0 :(得分:0)

因此,似乎从init调用Scroll方法并不起作用。但它确实起作用了。这是工作代码:

import wx
class AScrolledWindow(wx.ScrolledWindow):
    def __init__(self, parent):
        self.parent = parent
        wx.ScrolledWindow.__init__(self, parent, -1, style=wx.TAB_TRAVERSAL)
        box = wx.BoxSizer(wx.VERTICAL)
        self.sizer = box
        self._labels = []
        self.button = wx.Button(self, -1, "Scroll Me")
        box.Add(self.button)
        self.Bind(wx.EVT_BUTTON,  self.OnClickTop, self.button)
        for y in xrange(1,30):
            self._labels.append(wx.StaticText(self, -1, "Label #%d" % (y,)))
            box.Add(self._labels[-1])
        self.SetSizer(self.sizer)
        fontsz = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT).GetPixelSize()
        self.SetScrollRate(fontsz.x, fontsz.y)
        self.EnableScrolling(True,True)

    def OnClickTop(self, event):
        self.Scroll(0, 50)

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


def main():
    wxapp = wx.App()
    fr = TestFrame()
    fr.Show(True)
    fr.ascrolledwindow.Scroll(0,50)
    wxapp.MainLoop()

if __name__=='__main__':
    main()