How to insert into listbox using wxPython

David Bolen db3l at fitlinxx.com
Mon Jan 12 11:34:33 EST 2004


"Andrew" <na> writes:

> I typed what you wrote
> 
> for x in self.results:
>     self.listbox.Append(x[0])
> 
> and I got the same error as what I had tried earlier
> 
> Traceback (most recent call last):
>   File "S:\GUI\MYSQL\mysqlgui.py", line 65, in OnB2Button
>     self.listbox.Append(x[0])
>   File "F:\Python22\Lib\site-packages\wxPython\controls.py", line 78, in
> Append
>     val = controlsc.wxControlWithItems_Append(self, *_args, **_kwargs)
> TypeError: String or Unicode type required
> 
> Any help is alway's appreciated

Your database query is returning a list of tuples, where each element
in the tuple is a column from your database that is part of the query
(or all columns in the table with your query of *).  The tuple is not
a string, which is what the wxListBox understands how to display.

I expect that if you change the code to:

    self.listbox.Append(str(x[0]))

you'll get rid of the error, since that will provide a string
representation of the tuple x[0], but I also expect it won't be
exactly what you want depending on the database columns, and/or the
way certain data types automatically turn themselves into strings.

In the end you'll probably want to process each entry in 'results'
according to your own desires for display purposes, formatting an
appropriate string to be put into the ListBox.  You may also find that
using a wxListCtrl in wxLC_REPORT mode fits well since it will make it
simpler to divide the columns of data (either that or a wxGrid,
although wxGrid is probably overkill).

-- David



More information about the Python-list mailing list