[wxPython] wxList Ctrl using Objects and no images

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] wxList Ctrl using Objects and no images

Jethro Cramp
Dear All,

I have a list of items which I read from a text file. Each line of the text
file is parsed into an object and then there is an object that manages the
list of objects.

I am trying to use this list of objects to fill a list control. Each line in
the list is filled by one object. Each column is one of the attributes of
the object.

I have had no problem using the wxList control much as in the Demo using
random numbers to fill the rows and columns but everytime I try to use
objects I hit a brick wall.

I don't want to use any images in the list.

To illustrate my problem written the following small script with the same
logic as the program I am trying to write. The script always fails on the
InsertStringItem command no matter what I do (And I've spent most evenings
over the last week or so trying a lot of things) it fails here.

Very grateful of all hints and comments.

Jethro Cramp
[hidden email]

#---------------------<begin>------------------------------
from wxPython.wx import *

class TestObj:
    def __init__(self, number):
        self.col0 = 'No. %d' % number
        self.col1 = 'col 2 of %d' % number

class TestListObj:
    '''This object manages all the TestObj'''
    def __init__(self):

        self.dict_of_TestObj = {}
        self.list_of_TestObj = []
        for x in range(10):
            newTestObj = TestObj(x)
            self.dict_of_TestObj[x] = newTestObj
            self.list_of_TestObj.append(x)

class MyList(wxListCtrl):
    def __init__(self,parent):
        wxListCtrl.__init__(self,parent, -1, wxDefaultPosition,
wxDefaultSize, wxLC_REPORT|wxSUNKEN_BORDER)
        self.InsertColumn(0, "Column 1")
        self.InsertColumn(1, "Column 2")

        'Create the list of objects'
        myObj = TestListObj()

        'loop over all the TestObj in the TestListObj and try to fill the
list ctrl'
        for all in myObj.list_of_TestObj:
            'everything breaksdown on the next line'
            self.InsertStringItem(myObj[all],'%s' %
myObj.dict_of_TestObj[all].col0)
            self.SetStringItem(myObj[all], 1, '%s' %
myObj.dict_of_TestObj[all].col0)

        self.Format_ListCtrl
        self.SetColumnWidth(0, wxLIST_AUTOSIZE)
        self.SetColumnWidth(1, wxLIST_AUTOSIZE)
        self.SetDimensions(-1, -1, -1, -1, sizeFlags=wxSIZE_AUTO)
        self.Show(true)

class MyNotebook(wxNotebook):
    '''The widgets that are tested are placed on pages of this notebook'''
    def __init__(self, parent, id, title=''):
        wxNotebook.__init__(self, parent, id, name = title)

class MyFrame(wxFrame):
    """This class handles the main window for testing widgets"""
    def __init__(self, parent, id,title):
        wxFrame.__init__(self, parent, id, title, size=(625,500))
        self.Centre(wxBOTH)

        self.CreateStatusBar(1, wxST_SIZEGRIP)
        self.SetStatusText("This is the statusbar")

        nb = MyNotebook (self, -1)
        list = MyList(nb)
        nb.AddPage(list, 'Page 1')
        list.Show(true)
        self.Show(true)


class MyApp(wxApp):
    def OnInit(self):
        frame = MyFrame(NULL, -1, 'This is a test of GUI widgets')
        frame.Show(true)
        self.SetTopWindow(frame)
        return true
#---------------------------------------------------------------------------
def main():
    app = MyApp(0)
    app.MainLoop()
#----------------------------------</end>-----------------------------------
-


_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] wxList Ctrl using Objects and no images

Robin Dunn
> To illustrate my problem written the following small script with the same
> logic as the program I am trying to write. The script always fails on the
> InsertStringItem command no matter what I do (And I've spent most evenings
> over the last week or so trying a lot of things) it fails here.
>

How does it fail?  Does it raise an exception?  Does it crash?

If you had answered these questions yourself then you would know that it
fails because you are trying to access myObj as a list or dictionary, but it
has no __getitem__ method.

    self.InsertStringItem(myObj[all],
                          '%s' % myObj.dict_of_TestObj[all].col0)


This one works:

    self.InsertStringItem(all,
                          '%s' % myObj.dict_of_TestObj[all].col0)


--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users