I have a simple code that needs to output correctly the rows I'd like to pull out from a mysql query. Please see my code below. Don't know what really is wrong. I've tried to search on the Net but no luck.

sql="""SELECT * FROM users ORDER BY fname ASC LIMIT 5"""        
    cr.execute(sql)
    rows = cr.fetchall()

    self.list=wx.ListCtrl(panel,pos=(20,100),size=(355,130),
                          style=wx.LC_REPORT|wx.SUNKEN_BORDER|wx.LC_HRULES|wx.LC_VRULES)

    self.list.InsertColumn(0, 'Name')
    self.list.InsertColumn(1, 'Email')
    self.list.InsertColumn(2, 'Contact Number', width=150)

    self.list.SetColumnWidth(0, 100)
    self.list.SetColumnWidth(1, 200)
    self.list.SetColumnWidth(2, 100)

    n=0
    for r in rows:
        self.list.InsertStringItem()(n, r[0])
        self.list.SetStringItem(n, 1, r[1])
        self.list.InsertItem(n, 2, r[2])
        n+=1

I'm getting the error "return controls.ListCtrl_InsertStringItem(*args, **kwargs) TypeError: String or Unicode type required". Help from anyone is very much appreciated. Thanks

有帮助吗?

解决方案

In the for loop, you need to just use the SetStringItem command for every column except the first one. So instead of "self.list.InsertItem(n, 2, r[2])" it should be self.list.SetStringItem (n, 2, r[2])

Personally, I would use ObjectListView instead of ListCtrl. I wrote up a nice tutorial on using it with SQLAlchemy and SQLite here:

http://www.blog.pythonlibrary.org/2011/11/10/wxpython-and-sqlalchemy-an-intro-to-mvc-and-crud/

That might be helpful to you too.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top