Hi,
I'm working on little phone book program with wxPython and SQLite. I can get database populated and can retrieve back the phones and names, but when I do fill them in ListCtrl it throws cruel errors :)
Here is the retrieved data from database:
[(1, u'Steve', u'Giddy', u'0654743553'), (2, u'Majia', u'Ukonovic', u'65473032'), (3, u'Joseph', u'Ryanc', u'7663991109')]

When I replace this with another list of tuples, everything loads fine, so I guess something is wrong with this result !
and here is the error it throws:
TypeError: String or Unicode type required
File "F:\My Projects\Python\PhoneBook.py\mainfile.pyw", line 117, in <module>
app.MainLoop()
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7967, in MainLoop
wx.PyApp.MainLoop(self)
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7292, in MainLoop
return _core_.PyApp_MainLoop(*args, **kwargs)
File "F:\My Projects\Python\PhoneBook.py\mainfile.pyw", line 104, in OnAdd
self.OnPopulate(items)
File "F:\My Projects\Python\PhoneBook.py\mainfile.pyw", line 79, in OnPopulate
index = self.screen.InsertStringItem(sys.maxint, data[0])
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line 4761, in InsertStringItem
return _controls_.ListCtrl_InsertStringItem(*args, **kwargs)

Recommended Answers

All 7 Replies

Here is a function that populates the control

def OnPopulate(self, items):
        print items
        for data in items:
            print data
            index = self.screen.InsertStringItem(sys.maxint, data[0])
            self.screen.SetStringItem(index, 0, str(data[0]))
            self.screen.SetStringItem(index, 1, str(data[1]))
            self.screen.SetStringItem(index, 2, str(data[2]))
            self.screen.SetStringItem(index, 3, str(data[3]))

File "F:\My Projects\Python\PhoneBook.py\mainfile.pyw", line 79, in OnPopulate
index = self.screen.InsertStringItem(sys.maxint, data[0])

[(1, u'Steve', u'Giddy', u'0654743553'), (2, u'Majia', u'Ukonovic', u'65473032'), (3, u'Joseph', u'Ryanc', u'7663991109')]

If this list is "data" then data[0] is a tuple. If the first tuple is "data" then data[0] is an integer. Print data and type(data).

So, what I got wrong?
Items is a list of tuples and so data becomes a tuple and hence data[0] an integer (Index of a tuple). Is there anything wrong with that. How can I rectify that?

TypeError: String or Unicode type required

So this would probably work by converting to a string.

index = self.screen.InsertStringItem(sys.maxint, str(data[0]))

I'll give a try on that!
Thanks

Wow! It worked. It seem that the result was not string.
Thanks everyone who contributed to that invaluable help!
Cheers :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.