[wxpython-users] Problems on creating ListCtrls

David Anderson zerty.david at gmail.com
Wed Mar 26 16:25:06 PDT 2008


Hi, I'm trying to create a list ctrl that I will only use the events When a
line is selected, I'm editing the demo app but I can't add the itens, Look
at my code and please try to find the mistake:


class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, parent, ID, pos=3Dwx.DefaultPosition,
                 size=3Dwx.DefaultSize, style=3D0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.ListCtrlAutoWidthMixin.__init__(self)


class ListaDuplas(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1, style=3Dwx.WANTS_CHARS)

        tID =3D wx.NewId()

        sizer =3D wx.BoxSizer(wx.VERTICAL)

        if wx.Platform =3D=3D "__WXMAC__" and \
               hasattr(wx.GetApp().GetTopWindow(), "LoadDemo"):
            self.useNative =3D 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 =3D TestListCtrl(self, tID,
                                 style=3Dwx.LC_REPORT
                                 | wx.BORDER_NONE
                                 | wx.LC_SORT_ASCENDING)


        sizer.Add(self.list, 1, wx.EXPAND)
        self.PopulateList()

        # Now that the list exists we can init the other base class,
        # see wx/lib/mixins/listctrl.py
        self.itemDataMap =3D musicdata


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

        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)


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

    def PopulateList(self):
        self.list.InsertColumn(0, "Duplas")
        self.list.InsertColumn(1, "Erros", wx.LIST_FORMAT_RIGHT)
        items =3D musicdata.items()
        index =3D 0
        for key, data in items:
            self.list.SetStringItem(index, 1, data[1])
            self.list.SetStringItem(index, 2, data[2])
            self.list.SetItemData(index, key)
            index +=3D 1

        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        self.list.SetColumnWidth(2, 100)

        # show how to select an item
        self.list.SetItemState(5, wx.LIST_STATE_SELECTED,
wx.LIST_STATE_SELECTED)

        # show how to change the colour of a couple items
        item =3D self.list.GetItem(1)
        item.SetTextColour(wx.BLUE)
        self.list.SetItem(item)
        item =3D self.list.GetItem(4)
        item.SetTextColour(wx.RED)
        self.list.SetItem(item)

        self.currentItem =3D 0
    def OnItemSelected(self, event):
        ##print event.GetItem().GetTextColour()
        self.currentItem =3D event.m_itemIndex
        str =3D list.GetItemText(self.currentItem) + self.getColumnText(
self.currentItem, 1) + self.getColumnText(self.currentItem, 2)

        event.Skip()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200803=
26/f8879ef6/attachment-0001.htm


More information about the wxpython-users mailing list