0

我需要制作一个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()

我怎样才能做到这一点?

4

1 回答 1

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) 而不是 using ,其余代码将保持原样。

下面是修改后的代码:

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()
于 2015-03-07T17:37:12.760 回答