且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

面板内的 ScrolledPanel 未调整大小

更新时间:2022-11-02 14:27:56

我想我明白了.像往常一样,在添加或删除小部件时,您需要在父级上调用 Layout,在这种情况下,它是获得新子级的滚动面板.您还必须调用 SetupScrolling() 以便它可以重新计算有多少空间以及它是否需要滚动条.这是一个在 Windows 上适用于我的示例:

I think I figured it out. As usual, when adding or deleting widgets, you need to call Layout on the parent, which in this case is the scrolled panel that is getting new children. You also have to call SetupScrolling() so it can recalculate how much space there is and whether or not it needs scrollbars. Here's an example that works for me on Windows:

import wx
import wx.lib.scrolledpanel as scrolled

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(200,500))

        # Add a panel so it looks the correct on all platforms
        self.panel = wx.Panel(self, wx.ID_ANY)

        # --------------------
        # Scrolled panel stuff
        self.scrolled_panel = scrolled.ScrolledPanel(self.panel, -1, 
                                 style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel1")
        self.scrolled_panel.SetAutoLayout(1)
        self.scrolled_panel.SetupScrolling()

        words = "A Quick Brown Insane Fox Jumped Over the Fence and Ziplined to Cover".split()
        self.spSizer = wx.BoxSizer(wx.VERTICAL)
        for word in words:
            text = wx.TextCtrl(self.scrolled_panel, value=word)
            self.spSizer.Add(text)
        self.scrolled_panel.SetSizer(self.spSizer)
        # --------------------

        btn = wx.Button(self.panel, label="Add Widget")
        btn.Bind(wx.EVT_BUTTON, self.onAdd)

        panelSizer = wx.BoxSizer(wx.VERTICAL)
        panelSizer.AddSpacer(50)
        panelSizer.Add(self.scrolled_panel, 1, wx.EXPAND)
        panelSizer.Add(btn)
        self.panel.SetSizer(panelSizer)

    #----------------------------------------------------------------------
    def onAdd(self, event):
        """"""
        print "in onAdd"
        new_text = wx.TextCtrl(self.scrolled_panel, value="New Text")
        self.spSizer.Add(new_text)
        self.scrolled_panel.Layout()
        self.scrolled_panel.SetupScrolling()

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm().Show()
    app.MainLoop()