【问题标题】:Creating a scrolling panel in wxPython在 wxPython 中创建滚动面板
【发布时间】:2014-02-18 02:52:12
【问题描述】:

级别:初学者

我最近开始使用 wxPython 编写一个 GUI 应用程序。我在创建可滚动面板时遇到问题。我已经有一个运行良好的wx.Frame。我的 gui 中有 2 个面板。 (请暂时忽略面板 3)我想让面板 2 可滚动,以便它可以包含更多元素。我的 GUI 的基本结构如下:

我尝试在我的代码中使用wx.lib.scrolledpanel.ScrolledPanel(),但由于某种原因没有出现滚动条。我的代码如下:

panel2 = wx.lib.scrolledpanel.ScrolledPanel(self,-1, size=(600,400), pos=(0,28), style=wx.SIMPLE_BORDER)
panel2.SetupScrolling()
button1 = wx.Button(panel2,label="Button 1",pos=(0,50),size=(50,50))
button2 = wx.Button(panel2,label="Button 2",pos=(0,100), size=(50,50))
button3 = wx.Button(panel2,label="Button 3",pos=(0,150),size=(50,50))
button4 = wx.Button(panel2,label="Button 4",pos=(0,200), size=(50,50))
button5 = wx.Button(panel2,label="Button 5",pos=(0,250),size=(50,50))
button6 = wx.Button(panel2,label="Button 6",pos=(0,300), size=(50,50))
button7 = wx.Button(panel2,label="Button 7",pos=(0,350), size=(50,50))
button8 = wx.Button(panel2,label="Button 8",pos=(0,400), size=(50,50))

目前,当我执行我的代码时,我得到的 panel-2 只有 7 个按钮而不是 8 个。我希望第 8 个按钮会创建滚动条,因为它不适合 panel-2 的尺寸。 任何人都可以建议我的问题可能的解决方案还是我遗漏了什么?

感谢您的宝贵时间和 PS:有一个类似的问题here没有回答。

完整的代码可以在下面找到:

import wx
import wx.lib.scrolledpanel

    class GUI(wx.Frame):

        def __init__(self,parent,id,title):
            #First retrieve the screen size of the device
            screenSize = wx.DisplaySize()
            screenWidth = screenSize[0]
            screenHeight = screenSize[1]

            #Create a frame
            wx.Frame.__init__(self,parent,id,title,size=screenSize, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)

            panel1 = wx.Panel(self,size=(screenWidth,28), pos=(0,0), style=wx.SIMPLE_BORDER)
            panel1.SetBackgroundColour('#FDDF99')
            panel2 = wx.lib.scrolledpanel.ScrolledPanel(self,-1, size=(screenWidth,400), pos=(0,28), style=wx.SIMPLE_BORDER)
            panel2.SetupScrolling()
            panel2.SetBackgroundColour('#FFFFFF')
            button1 = wx.Button(panel2,label="Button 1",pos=(0,50),size=(50,50))
            button2 = wx.Button(panel2,label="Button 2",pos=(0,100), size=(50,50))
            button3 = wx.Button(panel2,label="Button 3",pos=(0,150),size=(50,50))
            button4 = wx.Button(panel2,label="Button 4",pos=(0,200), size=(50,50))
            button5 = wx.Button(panel2,label="Button 5",pos=(0,250),size=(50,50))
            button6 = wx.Button(panel2,label="Button 6",pos=(0,300), size=(50,50))
            button7 = wx.Button(panel2,label="Button 7",pos=(0,350), size=(50,50))
            button8 = wx.Button(panel2,label="Button 8",pos=(0,400), size=(50,50))


    if __name__=='__main__':
        app = wx.App()
        frame = GUI(parent=None, id=-1, title="Test")
        frame.Show()
        app.MainLoop()

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    您可以将大小调整器添加到滚动面板中以包含所有按钮。 这些代码应该可以工作:

    import wx
    import wx.lib.scrolledpanel
    
    class GUI(wx.Frame):
    
        def __init__(self,parent,id,title):
            #First retrieve the screen size of the device
            screenSize = wx.DisplaySize()
            screenWidth = screenSize[0]
            screenHeight = screenSize[1]
        
            #Create a frame
            wx.Frame.__init__(self,parent,id,title,size=screenSize, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
    
            panel1 = wx.Panel(self,size=(screenWidth,28), pos=(0,0), style=wx.SIMPLE_BORDER)
            panel1.SetBackgroundColour('#FDDF99')
            panel2 = wx.lib.scrolledpanel.ScrolledPanel(self,-1, size=(screenWidth,400), pos=(0,28), style=wx.SIMPLE_BORDER)
            panel2.SetupScrolling()
            panel2.SetBackgroundColour('#FFFFFF')
    
    
            button1 = wx.Button(panel2,label="Button 1",pos=(0,50),size=(50,50))
            button2 = wx.Button(panel2,label="Button 2",pos=(0,100), size=(50,50))
            button3 = wx.Button(panel2,label="Button 3",pos=(0,150),size=(50,50))
            button4 = wx.Button(panel2,label="Button 4",pos=(0,200), size=(50,50))
            button5 = wx.Button(panel2,label="Button 5",pos=(0,250),size=(50,50))
            button6 = wx.Button(panel2,label="Button 6",pos=(0,300), size=(50,50))
            button7 = wx.Button(panel2,label="Button 7",pos=(0,350), size=(50,50))
            button8 = wx.Button(panel2,label="Button 8",pos=(0,400), size=(50,50))
    
    
    
            bSizer = wx.BoxSizer( wx.VERTICAL )
            bSizer.Add( button1, 0, wx.ALL, 5 )
            bSizer.Add( button2, 0, wx.ALL, 5 )
            bSizer.Add( button3, 0, wx.ALL, 5 )
            bSizer.Add( button4, 0, wx.ALL, 5 )
            bSizer.Add( button5, 0, wx.ALL, 5 )
            bSizer.Add( button6, 0, wx.ALL, 5 )
            bSizer.Add( button7, 0, wx.ALL, 5 )
            bSizer.Add( button8, 0, wx.ALL, 5 )
            panel2.SetSizer( bSizer )
    
    if __name__=='__main__':
        app = wx.App()
        frame = GUI(parent=None, id=-1, title="Test")
        frame.Show()
        app.MainLoop()
    

    【讨论】:

    • 完美!我试图用拳击手做同样的事情,但它对我不起作用。非常感谢。 :)
    • 详细说明(因为@user3239580 击败了我 :))您没有获得滚动条的原因是因为您的滚动面板从未调整为窗口大小。这就是 sizer 所做的。
    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2014-06-25
    相关资源
    最近更新 更多