TreeCtrl GetNextVisible

Saketh Bhamidipati saketh.bhamidipati at gmail.com
Fri Sep 1 06:56:12 PDT 2006


A method of mine iterates through a TreeCtrl and writes data to an XML file
using ElementTree. In order to move the iterator forward, I use
GetNextVisible. When writing this method, I did not know that the node
actually has to be visible for GetNextVisible to return it. This is causing
problems for me now that I have many nodes in a TreeCtrl that require a
scrollbar.

I still want to iterate through the TreeCtrl, but without the major issue
that all nodes must be visible for the iteration to work.

Fortunately, my TreeCtrl has but one level of children. So a sample tree
would look like this:

- Animals
Cat
Rat
Gnat

I want to use the same iteration, but without the problems that
GetNextVisible gives me.

Here is the method in question:

    def WriteState(self, filename):
        """
        Iterate through the nodes, putting the XML'ed text into a specified
file.
        """
        current =3D self.tree.GetRootItem()
        root =3D Element("tree")
        root.set("title", self.tree.GetItemText(self.tree.GetRootItem()))
        roottext =3D unicode(self.tree.GetPyData(self.tree.GetRootItem()))
        if roottext =3D=3D 'None':
            roottext =3D "" # We don't want it to literally write 'None'
        treedata =3D SubElement(root, "treedata")
        treedata.text =3D roottext

        # Loop through children now
        while current.IsOk():
            # The offending GetNextVisible...what to replace it with?
            new =3D self.tree.GetNextVisible(current)
            current =3D new
            if not current.IsOk(): break

            # Start translating the data
            new =3D SubElement(root, "node")
            new.set("title", unicode(self.tree.GetItemText
(current)).encode('utf8'))

            data =3D SubElement(new, "data")
            datatext =3D unicode(self.tree.GetPyData(current)).encode('utf8=
')
            if datatext =3D=3D 'None':
                        datatext =3D "" # Again, we don't want it to litera=
lly
write 'None'
            data.text =3D datatext

        tree =3D ElementTree(root)
        tree.write(unicode(filename))

Once again, it works fine when all nodes are visible, but when there are too
many nodes to be all visible at once the method fails.

Thanks,
Saketh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200609=
01/af62d42f/attachment.htm


More information about the wxpython-users mailing list