【问题标题】:How to scroll Panel with GridBagSizer in wxPython如何在 wxPython 中使用 GridBagSizer 滚动面板
【发布时间】:2015-03-07 15:56:27
【问题描述】:

我需要使Panel 可滚动。我用过GridBagSizer

代码:

import wx

class MyFrame( wx.Frame ):
    def __init__( self, parent, ID, title ):
        wx.Frame.__init__( self, parent, ID, title, wx.DefaultPosition, wx.Size( 400, 300 ) )

        self.InitUI()
        self.Center()
        self.Show()


    def InitUI(self):

        MPanel = wx.Panel(self)
        GridBag = wx.GridBagSizer(2, 2)


        CO = ["RED", "BLUE"]

        for i in range(10):

            X = wx.StaticText(MPanel, size=(50,50), style=wx.ALIGN_CENTER, label="")
            X.SetBackgroundColour(CO[i%2])
            GridBag.Add(X, pos=(i+1, 1), flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=1)

        GridBag.AddGrowableCol(1)
        MPanel.SetSizerAndFit(GridBag)

class MyApp( wx.App ):
    def OnInit( self ):
        self.fr = MyFrame( None, -1, "K" )
        self.fr.Show( True )
        self.SetTopWindow( self.fr )
        return True


app = MyApp( 0 )
app.MainLoop()

我该怎么做?

【问题讨论】:

    标签: python wxpython scrollable gridbagsizer


    【解决方案1】:

    如果以下解决方案对您有帮助,请尝试:

    import wx
    import wx.lib.scrolledpanel as scrolled
    class MyPanel(scrolled.ScrolledPanel):
    
        def __init__(self, parent):
            scrolled.ScrolledPanel.__init__(self, parent, -1)
            self.SetAutoLayout(1)
            self.SetupScrolling()
    

    现在不要使用 MPanel=wx.Panel(self),而是使用 `MPanel = MyPanel(self),其余代码将保持原样。

    下面是修改后的代码:

    class MyFrame( wx.Frame ):
        def __init__( self, parent, ID, title ):
            wx.Frame.__init__( self, parent, ID, title, wx.DefaultPosition, wx.Size( 400, 300 ) )
    
            self.InitUI()
            self.Center()
            self.Show()
    
        def InitUI(self):
    
            MPanel = MyPanel(self)
            GridBag = wx.GridBagSizer(2, 2)
    
            CO = ["RED", "BLUE"]
    
            for i in range(10):
                X = wx.StaticText(MPanel, size=(50,50), style=wx.ALIGN_CENTER, label="")
                X.SetBackgroundColour(CO[i%2])
                GridBag.Add(X, pos=(i+1, 1), flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=1)
    
            GridBag.AddGrowableCol(1)
            MPanel.SetSizerAndFit(GridBag)
    
    class MyApp( wx.App ):
        def OnInit( self ):
            self.fr = MyFrame( None, -1, "K" )
            self.fr.Show( True )
            self.SetTopWindow( self.fr )
            return True
    
    app = MyApp( 0 )
    app.MainLoop()
    

    【讨论】:

    • 当我将此滚动面板添加到另一个 wx.Panel() 中时,它不起作用。这意味着,我在一个面板中有两个面板,一个可以滚动,另一个不能滚动跨度>
    猜你喜欢
    • 2011-10-31
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多