简体   繁体   中英

How to put ListCtrl in ListBook in wxPython?

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.

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. Ie 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.

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