Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:50:40+00:00 2026-06-11T23:50:40+00:00

I am new to wxPython. I can build a ListCtrl(a Demo of wxPython)(TestVirtualList.py), import

  • 0

I am new to wxPython.

I can build a ListCtrl(a Demo of wxPython)(TestVirtualList.py),

import  wx
import sys

#----------------------------------------------------------------------

class TestVirtualList(wx.ListCtrl):
    def __init__(self, parent, log):
        wx.ListCtrl.__init__(
            self, parent, -1, 
            style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES
            )

        self.log = log

        self.InsertColumn(0, "First")
        self.InsertColumn(1, "Second")
        self.InsertColumn(2, "Third")
        self.SetColumnWidth(0, 175)
        self.SetColumnWidth(1, 175)
        self.SetColumnWidth(2, 175)

        self.SetItemCount(100)

        self.attr1 = wx.ListItemAttr()
        self.attr1.SetBackgroundColour("yellow")

        self.attr2 = wx.ListItemAttr()
        self.attr2.SetBackgroundColour("light blue")

        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
        self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)


    def OnItemSelected(self, event):
        self.currentItem = event.m_itemIndex
        self.log.write('OnItemSelected: "%s", "%s", "%s", "%s"\n' %
                           (self.currentItem,
                            self.GetItemText(self.currentItem),
                            self.getColumnText(self.currentItem, 1),
                            self.getColumnText(self.currentItem, 2)))

    def OnItemActivated(self, event):
        self.currentItem = event.m_itemIndex
        self.log.write("OnItemActivated: %s\nTopItem: %s\n" %
                           (self.GetItemText(self.currentItem), self.GetTopItem()))

    def getColumnText(self, index, col):
        item = self.GetItem(index, col)
        return item.GetText()

    def OnItemDeselected(self, evt):
        self.log.write("OnItemDeselected: %s" % evt.m_itemIndex)

    def OnGetItemText(self, item, col):
        return "Item %d, column %d" % (item, col)




class TestVirtualListPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)

        self.log = log
        sizer = wx.BoxSizer(wx.VERTICAL)

        if wx.Platform == "__WXMAC__" and \
               hasattr(wx.GetApp().GetTopWindow(), "LoadDemo"):
            self.useNative = wx.CheckBox(self, -1, "Use native listctrl")
            self.useNative.SetValue( 
                not wx.SystemOptions.GetOptionInt("mac.listctrl.always_use_generic") )
            self.Bind(wx.EVT_CHECKBOX, self.OnUseNative, self.useNative)
            sizer.Add(self.useNative, 0, wx.ALL | wx.ALIGN_RIGHT, 4)

        self.list = TestVirtualList(self, self.log)
        sizer.Add(self.list, 1, wx.EXPAND)

        self.SetSizer(sizer)
        self.SetAutoLayout(True)

    def OnUseNative(self, event):
        wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", not event.IsChecked())
        wx.GetApp().GetTopWindow().LoadDemo("ListCtrl_virtual")

#----------------------------------------------------------------------

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    win = wx.Frame(parent = None, title = 'Simple Editor', size = (600, 400))
    log = sys.stdout
    bkg = TestVirtualListPanel(win, log)
    app.SetTopWindow(win)
    win.Show()
    app.MainLoop()
#----------------------------------------------------------------------

and I can build a ListBook(TestLB.py),

import wx
import sys


colourList = [ "Aquamarine", "Black", "Blue", "Blue Violet", "Brown", "Cadet Blue",
               "Coral", "Cornflower Blue", "Cyan", "Dark Grey", "Dark Green",
               "Dark Olive Green",
               ]

#----------------------------------------------------------------------------

class TestLB(wx.Listbook):
    def __init__(self, parent, id, log):
        wx.Listbook.__init__(self, parent, id, style=
                            wx.BK_DEFAULT
                            #wx.BK_TOP
                            #wx.BK_BOTTOM
                            #wx.BK_LEFT
                            #wx.BK_RIGHT
                            )
        self.log = log

        # make an image list using the LBXX images
        il = wx.ImageList(32, 32)
        tsize = (32, 32)
        for x in range(12):
            bmp = wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR, tsize)
            il.Add(bmp)
        self.AssignImageList(il)

        # Now make a bunch of panels for the list book
        first = True
        imID = 0
        for colour in colourList:
            win = self.makeColorPanel(colour)
            self.AddPage(win, colour, imageId=imID)
            imID += 1
            if imID == il.GetImageCount(): imID = 0
            if first:
                st = wx.StaticText(win, -1,
                          "I want the LISTCRL to show here,\n"
                          "This is the LISTCTRL %d, how can I do this???" % (imID),
                          wx.Point(10, 10))
                first = True

        self.Bind(wx.EVT_LISTBOOK_PAGE_CHANGED, self.OnPageChanged)
        self.Bind(wx.EVT_LISTBOOK_PAGE_CHANGING, self.OnPageChanging)


    def makeColorPanel(self, color):
        p = wx.Panel(self, -1)
        return p


    def OnPageChanged(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.GetSelection()
        self.log.write('OnPageChanged,  old:%d, new:%d, sel:%d\n' % (old, new, sel))
        event.Skip()

    def OnPageChanging(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        sel = self.GetSelection()
        self.log.write('OnPageChanging, old:%d, new:%d, sel:%d\n' % (old, new, sel))
        event.Skip()

#----------------------------------------------------------------------------

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    win = wx.Frame(parent = None, title = 'Simple Editor', size = (600, 400))
    log = sys.stdout
    booklist = TestLB(win, -1, log)
    app.SetTopWindow(win)
    win.Show()
    app.MainLoop()

Now I want to put ListCtrls in each page of ListBook, how can I do that?

I have tried to modify the OnPageChanged() function in TestLB.py:

def OnPageChanged(self, event):
    old = event.GetOldSelection()
    new = event.GetSelection()
    sel = self.GetSelection()
    self.log.write('OnPageChanged,  old:%d, new:%d, sel:%d\n' % (old, new, sel))

    win = self.makePanel()
    sizer = wx.BoxSizer(wx.VERTICAL)

    object_list = ObjectList(self, self.log)
    sizer.Add(object_list, 1, wx.EXPAND)


    win.SetSizer(sizer)
    self.SetAutoLayout(True)
    self.AddPage(win, bucketList[new], imageId=new)

    event.Skip()

bucketList is a list and each element is a string. But it did not work as expected. The ListCtrl did not display on the Panel(the right part of the ListBook), but on the whole ListBook. So how can I make it work as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:50:41+00:00Added an answer on June 11, 2026 at 11:50 pm

    If you want to add page contents dynamically, you can do it, but usually it’s much simpler to just call AddPage() immediately after wxListbook construction. I.e. there is really no need to handle any events, just create and add all the pages (which are simply arbitrary windows) when you create the book control itself.

    Also, I don’t know where is your makePanel function but you need to create pages with the book itself as the parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to wxPython. Can someone help me in: How to add multiple
I want to add a new feature in a wxPython class. I also want
I've written code for ListCtrl in wxPython: ... self.list_ctrl = wx.ListCtrl(panel, size=(-1,150), style=wx.LC_REPORT|wx.BORDER_SUNKEN) self.list_ctrl.InsertColumn(0,
I'm relatively new to wxPython (but not Python itself), so forgive me if I've
I am fairly new to programming and to python and wxpython. I have looked
new to c#. I'm trying to make a simple system where I can search
How can I store button, listbox and other wxpython widgets in an array? My
Is it better to do it all at once? I'm very new to wxPython
Hi I'm sort new to wxPython and still in the process of learning. I'm
In wxpython I can use a staticbox in combination with a staticboxsizer to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.