felipi
felipi

Reputation: 149

AttributeError: 'ListCtrl' object has no attribute 'insert'

I want to list my bank in listctrl, but give this error, can anyone help me?

I've tried only that which is what I know, I could not find information on how to correct the error.

import wx
import sqlite3

class MyForm(wx.Frame):

   db_name = 'banco.db'

  def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")

    # Add a panel so it looks the correct on all platforms
    panel = wx.Panel(self, wx.ID_ANY)
    self.index = 0

    self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
                     style=wx.LC_REPORT
                     |wx.BORDER_SUNKEN
                     )
    self.list_ctrl.InsertColumn(0, 'User')
    self.list_ctrl.InsertColumn(1, 'Senha')
    #self.list_ctrl.InsertColumn(2, 'Location', width=125)

    #btn = wx.Button(panel, label="Add Line")
    #btn2 = wx.Button(panel, label="Get Data")
    #btn.Bind(wx.EVT_BUTTON, self.add_line)
    #btn2.Bind(wx.EVT_BUTTON, self.get_data)

    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)

    self.get_users()

def run_query (self, query, parameters=()):
    with sqlite3.connect(self.db_name) as conn:
        cursor = conn.cursor()
        result = cursor.execute(query, parameters)
        conn.commit()
    return result

def get_users(self):
    # Analisando dados
    query = 'SELECT * FROM users'
    db_rows= self.run_query(query)
    # Preenchimento de dados
    for row in db_rows:
        self.list_ctrl.InsertItem(row[0],row[1],row[2])

# Run the program
if __name__ == "__main__":
  app = wx.App(False)
  frame = MyForm()
  frame.Show()
  app.MainLoop()

I hope someone can help me, because I do not know what else to do.

Upvotes: 0

Views: 482

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22453

You almost certainly, in this case, want ListCtrl.Append().
As in:

self.list_ctrl.Append((row[0],row[1]))

Upvotes: 1

BoarGules
BoarGules

Reputation: 16941

wx.ListCtrl objects don't have an .insert() method, as the error message says. A ListCtrl isn't a Python list. You probably want .Append() or .InsertItem(). More details here.

The docs for .Append() say

Append(self, entry) Append an item to the list control. The entry parameter should be a sequence with an item for each column

Here it may not be clear that item means an instance of a wx.ListItem object. But I did refer you also to .InsertItem() which makes that very clear. wxPython has a steep learning curve, even for programmers experienced both in Python and in other GUI frameworks, and dealing with little gaps like that is part of the curve.

Your control has 2 (or, with one uncommented) 3 columns, so as entry you need to pass a sequence, that is a list or a tuple, consisting of 2 (or 3) wx.ListItem objects. If you don't know how to do that then I suggest you study the demo application that comes with wxPython, or look at wxGlade, which will build the wx objects for you.

I've no idea what you plan to do with this control, but do bear in mind that it is an extremely powerful and complex one with optional icons of various sizes and a choice of single- or multicolumn display. If you don't need these features you might be better off with a wx.grid.Grid instead.

Upvotes: 0

Related Questions