1

I want to design a wxpython ListCtrl. So when the Search button is clicked i am getting list like this

    [(7, u'GLUOCOSE', u'C6H1206'), (8, u'SUCROSE', u'C12H22O11')]

I want to populate the listctrl with the above output. I am InsertStringItem method, which will return me the index of current row and rest of the columns are filled by SetStringItem() method.But that gives me TypeError: String or Unicode type required.How do i accomplish this?

def OnSearch(self, event):        
    placeholder = '?'
    placeholders = ','.join(placeholder for unused in self.molecule_list)
    query = 'SELECT * FROM MOLECULE WHERE MOL_NUMBER IN (%s)' % placeholders
    cursor = self.conn.execute(query, self.molecule_list)
    final = cursor.fetchall()
    print final 
    for j in final: 
        index = self.list.InsertStringItem(sys.maxint, j[0]) 
        self.list.SetStringItem(index, 1, j[1]) 
        self.list.SetStringItem(index, 2, j[2]) 

1 Answer 1

1

You are iterating over the cursor, rather than the data which was returned in the variable final. Swap over to iterate using final

index = 0
for j in final: 
    index = self.list.InsertStringItem(index, str(j[0])) 
    self.list.SetStringItem(index, 1, j[1]) 
    self.list.SetStringItem(index, 2, j[2])
    index +=1 

The easiest way, is to ensure that the ListCtrl has a style=wx.LC_REPORT and then use Append.

for j in final:
    self.list.Append((j[0],j[1],j[2],j[3]))

Where j[n] is each item you require from the data for the number of items in the ListCtrl. I hope that makes sense.

Here is an example showing both methods

import wx
class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Molecules")
        panel = wx.Panel(self, wx.ID_ANY)
        self.index = 0
        self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
                         style=wx.LC_REPORT
                         )
        self.list_ctrl.InsertColumn(0, 'Number')
        self.list_ctrl.InsertColumn(1, 'Element')
        self.list_ctrl.InsertColumn(2, 'Make up')

        btn = wx.Button(panel, label="Add data")
        btn.Bind(wx.EVT_BUTTON, self.add_lines)
        btn2 = wx.Button(panel, label="Append data")
        btn2.Bind(wx.EVT_BUTTON, self.app_lines)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(btn2, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

    def add_lines(self, event):
        data = [[7, 'GLUCOSE', 'C6H1206'],[8,'SUCROSE', 'C12H22O11']]
        index = 0
        for j in data:
            self.list_ctrl.InsertStringItem(index, str(j[0]))
            self.list_ctrl.SetStringItem(index, 1, j[1])
            self.list_ctrl.SetStringItem(index, 2, j[2])
            index += 1
    def app_lines(self, event):
        data = [[7, 'GLUCOSE', 'C6H1206'],[8,'SUCROSE', 'C12H22O11']]
        for j in data:
            self.list_ctrl.Append((j[0],j[1],j[2]))

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
Sign up to request clarification or add additional context in comments.

4 Comments

I got the answer but when i try this using InsertStringItem that gives me TypeError: String or Unicode type required. and i am inserting a string
Without being able to see the error or the listctrl it's difficult to say but the structure should be InsertStringItem(index, label, imageIndex=-1). Using sys.maxint for every index is very WRONG. Try the append method, it's easier.
For the index, initialise it before the loop to zero and increment within the loop. That gives you a fighting chance of that code working.
Thanks are appreciated, perhaps an accept or vote up of the answer is appropriate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.