wx.listctrl- insertstringitem not working

Larry Bates larry.bates at websafe.com
Fri Nov 9 17:27:38 EST 2007


barronmo wrote:
> Newbie with problem.  I'm trying to build a multicolumn list control
> with wxPython and am having difficulty getting the data from my query
> into each column.  I'm getting the following error:
> 
> Traceback (most recent call last):
>   File "/home/mb/PyPrograms/EMRGUI/Selectable.py", line 115, in
> <module>
>     Repository(None, -1, 'Repository')
>   File "/home/mb/PyPrograms/EMRGUI/Selectable.py", line 57, in
> __init__
>     index = self.list.InsertStringItem(sys.maxint, i['patient_ID'])
>   File "/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/lib/
> mixins/listctrl.py", line 751, in __InsertStringItem_
>     index = self.InsertImageStringItem(index, label, 0)
>   File "/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/
> _controls.py", line 4716, in InsertImageStringItem
>     return _controls_.ListCtrl_InsertImageStringItem(*args, **kwargs)
> TypeError: String or Unicode type required
> 
> 
> The code I'm using is based on the wxPython tutorial at Zetcode:
> 
> #!/usr/bin/python
> 
> 
> import wx
> import sys
> import MySQLdb
> from wx.lib.mixins.listctrl import CheckListCtrlMixin,
> ListCtrlAutoWidthMixin
> 
> conn = MySQLdb.connect(host = "localhost",
>           user = "root",
>           passwd = "Barron85",
>           db = "meds")
> 
> def name_find(namefrag):
> 
>      cursor = conn.cursor(MySQLdb.cursors.DictCursor)
>      cursor.execute("SELECT patient_ID, firstname, lastname,
> phonenumber, SSN, \
> 			DOB FROM demographics WHERE lastname LIKE '%s%%'" % (namefrag))
> 
>      results = cursor.fetchall()
>      results_list = list(results)
>      return results_list
>      cursor.close()
> 
> 
> 
> class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin,
> ListCtrlAutoWidthMixin):
>     def __init__(self, parent):
>         wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT |
> wx.SUNKEN_BORDER)
>         CheckListCtrlMixin.__init__(self)
>         ListCtrlAutoWidthMixin.__init__(self)
> 
> 
> class Repository(wx.Frame):
>     def __init__(self, parent, id, title):
>         wx.Frame.__init__(self, parent, id, title, size=(850, 400))
> 
>         panel = wx.Panel(self, -1)
> 
>         vbox = wx.BoxSizer(wx.VERTICAL)
>         hbox = wx.BoxSizer(wx.HORIZONTAL)
> 
>         leftPanel = wx.Panel(panel, -1)
>         rightPanel = wx.Panel(panel, -1)
> 
>         self.log = wx.TextCtrl(rightPanel, -1, style=wx.TE_MULTILINE)
>         self.list = CheckListCtrl(rightPanel)
>         self.list.InsertColumn(0, 'Patient ID', width=100)
>         self.list.InsertColumn(1, 'Lastname', width=200)
>         self.list.InsertColumn(2, 'Firstname', width=125)
> 	self.list.InsertColumn(3, 'Phone')
> 	self.list.InsertColumn(4, 'SSN')
> 	self.list.InsertColumn(5, 'DOB')
> 
> 	patients = name_find(str(raw_input('Select the first several letters
> of the last name: ')))
>         for i in patients:
>             index = self.list.InsertStringItem(sys.maxint,
> i['patient_ID'])
>             self.list.SetStringItem(index, 1, i['lastname'])
>             self.list.SetStringItem(index, 2, i['firstname'])
> 	    self.list.SetStringItem(index, 3, i['phonenumber'])
> 	    self.list.SetStringItem(index, 4, i['SSN'])
> 	    self.list.SetStringItem(index, 5, i['DOB'])
> 
>         vbox2 = wx.BoxSizer(wx.VERTICAL)
> 
>         sel = wx.Button(leftPanel, -1, 'Select All', size=(100, -1))
>         des = wx.Button(leftPanel, -1, 'Deselect All', size=(100, -1))
>         apply = wx.Button(leftPanel, -1, 'Apply', size=(100, -1))
> 
> 
>         self.Bind(wx.EVT_BUTTON, self.OnSelectAll, id=sel.GetId())
>         self.Bind(wx.EVT_BUTTON, self.OnDeselectAll, id=des.GetId())
>         self.Bind(wx.EVT_BUTTON, self.OnApply, id=apply.GetId())
> 
>         vbox2.Add(sel, 0, wx.TOP, 5)
>         vbox2.Add(des)
>         vbox2.Add(apply)
> 
>         leftPanel.SetSizer(vbox2)
> 
>         vbox.Add(self.list, 1, wx.EXPAND | wx.TOP, 3)
>         vbox.Add((-1, 10))
>         vbox.Add(self.log, 0.5, wx.EXPAND)
>         vbox.Add((-1, 10))
> 
>         rightPanel.SetSizer(vbox)
> 
>         hbox.Add(leftPanel, 0, wx.EXPAND | wx.RIGHT, 5)
>         hbox.Add(rightPanel, 1, wx.EXPAND)
>         hbox.Add((3, -1))
> 
>         panel.SetSizer(hbox)
> 
>         self.Centre()
>         self.Show(True)
> 
>     def OnSelectAll(self, event):
>         num = self.list.GetItemCount()
>         for i in range(num):
>             self.list.CheckItem(i)
> 
>     def OnDeselectAll(self, event):
>         num = self.list.GetItemCount()
>         for i in range(num):
>             self.list.CheckItem(i, False)
> 
>     def OnApply(self, event):
>         num = self.list.GetItemCount()
>         for i in range(num):
>             if i == 0: self.log.Clear()
>             if self.list.IsChecked(i):
>                 self.log.AppendText(self.list.GetItemText(i) + '\n')
> 
> app = wx.App()
> Repository(None, -1, 'Repository')
> app.MainLoop()
> 
> 
> 
> If you see what I'm doing wrong I'd be grateful for any help.
> 
> Mike
> 

You probably will want to post over at gmane.comp.python.wxpython.  A lot more 
wx people (including wxPython author) over there.

-Larry



More information about the Python-list mailing list