简体   繁体   中英

wxPython Vertical Scroll for Notebook Tab

I am trying to add a vertical scroll bar to one of my notebook tabs using wxPython. I have tried adding the window style wx.VSCROLL to a few different panels and I am not getting any luck. Here is my code:

import wx
import os
class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title=title, size=(500,200))

        main = wx.Panel(self)

        nb = wx.Notebook(main)

        loginPg = wx.Panel(nb)
        entryPg = wx.Panel(nb, style=wx.VSCROLL) //<--- This kind of works
        projectsPg = wx.Panel(nb)

        nb.AddPage(loginPg, "Login")
        nb.AddPage(entryPg, "Entry")
        nb.AddPage(projectsPg, "Projects")

        self.entTitle = titlePanel(entryPg, -1)
        self.entLabel = labelPanel(entryPg, -1)
        self.entDescription = descrPanel(entryPg, -1)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        entryPgBox = wx.BoxSizer(wx.VERTICAL)

        mainSizer.Add(nb, 1, wx.ALL|wx.EXPAND)

        entryPgBox.Add(self.entTitle, 0, wx.EXPAND)
        entryPgBox.Add(self.entLabel, 0, wx.EXPAND)
        entryPgBox.Add(self.entDescription, 0, wx.EXPAND)

        entryPg.SetAutoLayout(True)
        entryPg.SetSizer(entryPgBox)
        entryPgBox.Fit(entryPg)

        main.SetAutoLayout(True)
        main.SetSizer(mainSizer)
        mainSizer.Fit(main)
        self.Layout()
        self.Show()

app = wx.App(False)
frame = MyFrame(None, -1, 'Test')
frame.Show()
app.MainLoop()

My goal is to have the second tab "Entry" of my notebook vertically scrollable. By adding style=wx.VSCROLL to entryPg I get a scrollbar for that tab but it will not scroll down. How do I get my Notebook Tab entryPg to have a vertical scrollbar that will scroll down? Any help is greatly appreciated!

Use a wx.ScrolledWindow instead of a wx.Panel to get a vertical scroll bar.

Here's a minimal example:

import wx

class Frame ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( 600,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        sizer = wx.BoxSizer( wx.VERTICAL )

        self.notebook = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.login = wx.Panel( self.notebook, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.notebook.AddPage( self.login, u"Login", False )
        self.entry = wx.ScrolledWindow( self.notebook, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
        self.entry.SetScrollRate( 5, 5 )
        entry_sizer = wx.BoxSizer( wx.VERTICAL )

        self.text = wx.StaticText( self.entry, wx.ID_ANY, u"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.text.Wrap( 600 )
        entry_sizer.Add( self.text, 1, wx.ALL, 0 )


        self.entry.SetSizer( entry_sizer )
        self.entry.Layout()
        entry_sizer.Fit( self.entry )
        self.notebook.AddPage( self.entry, u"Entry", True )

        sizer.Add( self.notebook, 1, wx.EXPAND |wx.ALL, 0 )


        self.SetSizer( sizer )
        self.Layout()

        self.Centre( wx.BOTH )
        self.Show()

if __name__ == "__main__":
    app = wx.App()
    Frame(None)
    app.MainLoop()

Here the Entry page will have both a vertical and horizontal scroll bar. Note that the scrollbars will be active only if the contents of the widget overflows the currently displayed size. If you maximize this application window the sccrollbars will disappear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM