在wxpython中滚动几个面板有麻烦

时间:2012-07-17 22:07:52

标签: python wxpython wxwidgets

我希望能够有一组垂直面板保持相同的尺寸,但我希望有一个垂直滚动条,允许我在窗口中滚动。现在我正在使用wx.ScrolledPanel并添加面板并将它们放入sizer中,但屏幕上没有显示任何内容

import wx
import  wx.lib.scrolledpanel as scrolled
class josh(wx.Frame):
def __init__(self, parent, id):   
    wx.Frame.__init__(self, wx.GetApp().TopWindow, title='Title',size=(800,600))
    arr= ['Q1.......','Q2.......','Q3.......','Q4.......','Q5.......','Q6.......','Q7..............','Q8..........']
    scroll=scrolled.ScrolledPanel(self, -1)
    #scroll.SetScrollbars(1,1,1,1) 
    panelList = []
    commentList = []
    count = 1
    sizer = wx.BoxSizer(wx.VERTICAL)

    for p in arr: 
        panelList.append(wx.Panel(scroll ,-1, size = (800,100), style=wx.SUNKEN_BORDER))
        sizer.Add(panelList[count-1], 1, wx.EXPAND)
        wx.StaticText(panelList[count-1], -1, str(count), (5, 50), wx.DefaultSize) 
        commentList.append( wx.TextCtrl(panelList[count-1], style=wx.TE_MULTILINE,pos=(500, 20),size = (200,50)))
        count+=1



    self.SetAutoLayout(True)
    self.SetSizer(sizer)
    scroll.SetupScrolling()
if __name__=='__main__':
app=wx.App(False)
frame=josh(parent=None,id=-1)
frame.Show()
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

您将每个面板作为滚动窗口小部件的子窗口小部件。但是你将每个面板添加到sizer,然后错误地将Frame的sizer设置为名为“sizer”的sizer。

这是一种不匹配,几乎总会产生不良的视觉效果。

你应该设置scrolled.SetSizer(sizer) 或者制作一个新的sizer,比如称之为frameizer然后添加滚动到它。然后是self.SetSizer(framesizer),其中self是josh()帧。

除非子窗口小部件比ScrolledPanel大,否则ScrolledPanel不会显示它的滚动条。

# Broken down here to show sizer child-parent hiearchy 
# AND widget child-parent hiearchy need to be congruent

import wx
import wx.lib.scrolledpanel
scrolled = wx.lib.scrolledpanel #i use reload(...) in pyslices.py


class Panel(wx.Panel):
    def __init__(self, parent, color='black'):
        wx.Panel.__init__(self, parent, size=(800,100))

        # several "Panels" sized added together 
        # are bigger than ScrolledPanel size

        self.SetMinSize( (800, 100) )
        self.SetBackgroundColour( color )

        widget1 = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        widget2 = wx.TextCtrl(self, style=wx.TE_MULTILINE)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add( widget1, 1, wx.ALL | wx.EXPAND, 15 )
        sizer.Add( widget2, 1, wx.ALL | wx.EXPAND, 15 )

        self.SetSizer( sizer )


class BigPanel(wx.Panel):
    def __init__(self, parent):

        # at least one child widget or aggregate width 
        # of several child widgets has to be wider than 
        # ScrolledPanel to show its horizontal scroll bar

        # at least one child widget or aggregate height
        # of more then one child widget has to be "taller" than
        # the height of the ScrolledPanel for it to show
        # its vertical scroll bar

        # -------------------------------------V---V
        wx.Panel.__init__(self, parent, size=(800,800))
        # -------------------------------------^---^

        panel0 = Panel(self, 'black')
        panel1 = Panel(self, 'red')
        panel2 = Panel(self, 'green')
        panel3 = Panel(self, 'blue')
        panel4 = Panel(self, 'white')
        panel5 = Panel(self, 'purple')

        sizer = wx.BoxSizer(wx.VERTICAL)

        sizer.Add( panel0, 1, wx.ALL | wx.EXPAND, 15 )
        sizer.Add( panel1, 1, wx.ALL | wx.EXPAND, 15 )
        sizer.Add( panel2, 1, wx.ALL | wx.EXPAND, 15 )
        sizer.Add( panel3, 1, wx.ALL | wx.EXPAND, 15 )
        sizer.Add( panel4, 1, wx.ALL | wx.EXPAND, 15 )
        sizer.Add( panel5, 1, wx.ALL | wx.EXPAND, 15 )

        self.SetSizer( sizer )


class ScrolledPanel(scrolled.ScrolledPanel):
    def __init__(self, parent):
        # ---------------------------------------------------V---V
        scrolled.ScrolledPanel.__init__(self, parent, size=(400,400))
        # ---------------------------------------------------^---^

        bigpanel = BigPanel(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add( bigpanel, 1, wx.ALL | wx.EXPAND, 15 )

        self.SetSizer( sizer )
        self.SetAutoLayout(1)
        self.SetupScrolling()


class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = wx.GetApp().GetTopWindow(),
            title = 'Trouble with scrolling through several panels in wxpython',
            size = (500,400))

        scroll = ScrolledPanel(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add( scroll, 1, wx.ALL | wx.EXPAND, 15 )
        self.SetSizer( sizer )


if __name__ == '__main__':
    app = wx.GetApp()
    if not app: app = wx.App(0)
    frame = Frame()
    frame.Show()
    if not app.IsMainLoopRunning():
        app.MainLoop()