wxPython: wxGrid vs. wxListCtrl

Piet pit.grinja at gmx.de
Tue Jun 29 15:16:11 EDT 2004


Hello.
I am working on an XML editor that will not display the xml file as
plain text, but will rather work with a combination of a tree view for
the main element nodes and some kind of tabular view to view
attributes. By this way, the user (i.e. me) will only have the
opportunity to change textual contents and attribute values, but
neither element nor attribute names.
Building the tree view was not a problem, but I haven´t found a good
widget for the "grid view" component. wxGrid allows to edit the
content of the table (which will be the attribute values) but I
haven´t found a way to link a specific cell to a certain xml node.
This is important, because when the cell content changes I would like
to be able to directly communicate this change to the underlying xml
tree (in the tree component of the editor, this is already achieved).
wxListCtrl would be the second choice. THis where I first focused on
an wrote the following code:
class ListCtrlWindow(wxFrame):
    def __init__(self, parent, node):
        wxFrame.__init__(self, parent, -1,"Attribute list")
        attributeListCtrl = wxListCtrl(self,-1,style =
wxLC_REPORT|wxLC_VRULES|wxLC_HRULES|wxLC_EDIT_LABELS )

        numberOfAttributes = 0
        attributeNameList = []
        attributeListCtrl.InsertColumn(0,"Number",format=wxLIST_FORMAT_LEFT,
width=-1)
        for attribute in node.attributes.keys():
            attr = node.attributes.get(attribute)
            print attr.nodeName + "\n"
            attributeListCtrl.InsertColumn(numberOfAttributes+2,attr.nodeName,format=wxLIST_FORMAT_LEFT,
width=-1)
            +numberOfAttributes
            attributeNameList.append(attr.nodeName)
        for entry in attributeNameList:
            print entry + " " + str(attributeNameList.index(entry)) +
"\n"
        numberOfSiblings = 0
        if node.parentNode != None:
            childs = node.parentNode.childNodes
            numRows = 0
            for numberOfSiblings in range(len(childs)):
                print childs.item(numberOfSiblings).nodeType
                if childs.item(numberOfSiblings).nodeType == 1:
                    attributeListCtrl.InsertStringItem(numRows,str(numRows))
                    if childs.item(numberOfSiblings).attributes !=
None:
                        for attribute in
childs.item(numberOfSiblings).attributes.keys():
                            attr =
childs.item(numberOfSiblings).attributes.get(attribute)
                            if attributeNameList.count(attr.nodeName)
== 0:
                               
attributeNameList.append(attr.nodeName)
                           
attributeListCtrl.SetStringItem(numRows,attributeNameList.index(attr.nodeName)+1,attr.nodeValue)
                    numRows = numRows + 1

        attributeListCtrl.EnsureVisible(True)
        attributeListCtrl.SetColumnWidth(-1,-1)
But there I have two problems/questions: First, a very general one: I
would like to edit the contents of the table, but when I double click
on the respective line, only the first element is editable.
Second, I have not yet completely understood the "data structure"
behind a list item. Does each line of a listctrl represent a single
item? Is it possible to address the entries in the line in the same
was as "cells" of a "table row"? Can items which are located in
different lines but are positioned in the same "column" be selected
like grid cells which belong to one column?
To me it looks a little as if listctrl was mainly for displaying data
and not for editing. So do I have to use a wxGrid for the attribute
list and define my own mechanism to connect a cell to a data object?
Any hints are appreciated.
Regards 
Peter



More information about the Python-list mailing list