在wxPython中创建滚动面板

2024-06-16 09:13:00 发布

您现在位置:Python中文网/ 问答频道 /正文

级别:初学者

我最近开始用wxPython编程一个GUI应用程序。创建可滚动面板时遇到问题。我已经有一个工作正常的wx.Frame。在我的gui中有两个面板。(请暂时忽略panel-3)我想使panel-2可以滚动,以便它可以包含更多的元素。我的图形用户界面的基本结构如下:

GUI demo

我已经尝试在代码中使用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))

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

感谢您抽出时间&; 注:有一个类似的问题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()

Tags: 代码posself面板sizestylelibgui
1条回答
网友
1楼 · 发布于 2024-06-16 09:13:00

您可以在滚动面板中添加一个sizer以包含所有按钮。 这些代码应该有效:

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()

相关问题 更多 >