AttributeError: 'ListCtrl' object has no attribute 'insert'
AttributeError: 'ListCtrl' object has no attribute 'insert'
我想在 listctrl 中列出我的银行,但出现此错误,有人可以帮助我吗?
我只尝试了我所知道的,我找不到关于如何更正错误的信息。
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()
我希望有人能帮助我,因为我不知道还能做什么。
wx.ListCtrl
对象没有 .insert()
方法,如错误消息所述。 ListCtrl
不是 Python 列表。您可能需要 .Append()
或 .InsertItem()
。更多详情 here.
.Append()
的文档说
Append(self, entry)
Append an item to the list control. The entry parameter should be a sequence with an item for each column
这里可能不清楚item是指wx.ListItem
对象的实例。但我确实也向您推荐了 .InsertItem()
,这非常清楚。 wxPython
有一个陡峭的学习曲线,即使对于在 Python 和其他 GUI 框架中都有经验的程序员也是如此,处理像这样的小差距是曲线的一部分。
您的控件有 2 个(或一个未注释的)3 列,因此 entry
您需要传递一个 sequence,即列表或元组,由 2(或 3)wx.ListItem
个对象组成。如果您不知道该怎么做,那么我建议您研究 wxPython 附带的演示应用程序,或者查看 wxGlade,它将为您构建 wx
对象。
我不知道你打算用这个控件做什么,但请记住,它是一个非常强大和复杂的控件,带有各种大小的可选图标以及单列或多列显示的选择。如果您不需要这些功能,您最好使用 wx.grid.Grid
。
在这种情况下,您几乎可以肯定想要 ListCtrl.Append()
。
如:
self.list_ctrl.Append((row[0],row[1]))
我想在 listctrl 中列出我的银行,但出现此错误,有人可以帮助我吗?
我只尝试了我所知道的,我找不到关于如何更正错误的信息。
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()
我希望有人能帮助我,因为我不知道还能做什么。
wx.ListCtrl
对象没有 .insert()
方法,如错误消息所述。 ListCtrl
不是 Python 列表。您可能需要 .Append()
或 .InsertItem()
。更多详情 here.
.Append()
的文档说
Append(self, entry)
Append an item to the list control. The entry parameter should be a sequence with an item for each column
这里可能不清楚item是指wx.ListItem
对象的实例。但我确实也向您推荐了 .InsertItem()
,这非常清楚。 wxPython
有一个陡峭的学习曲线,即使对于在 Python 和其他 GUI 框架中都有经验的程序员也是如此,处理像这样的小差距是曲线的一部分。
您的控件有 2 个(或一个未注释的)3 列,因此 entry
您需要传递一个 sequence,即列表或元组,由 2(或 3)wx.ListItem
个对象组成。如果您不知道该怎么做,那么我建议您研究 wxPython 附带的演示应用程序,或者查看 wxGlade,它将为您构建 wx
对象。
我不知道你打算用这个控件做什么,但请记住,它是一个非常强大和复杂的控件,带有各种大小的可选图标以及单列或多列显示的选择。如果您不需要这些功能,您最好使用 wx.grid.Grid
。
在这种情况下,您几乎可以肯定想要 ListCtrl.Append()
。
如:
self.list_ctrl.Append((row[0],row[1]))