Python listctrl未正确填充

Python listctrl未正确填充,python,wxpython,Python,Wxpython,因此,我设法以给定的顺序获得项目列表,以便正确填充数据库。转换项目以填充显示订单中包含的项目的listctrl小部件时。。。。。。。我明白了: 左侧窗口显示添加第一项时发生的情况。。。。右窗是我的问题所在。它实质上覆盖了第一项的文本,并添加了一个空白列。。。。但是,价格加起来是正确的,所有项目都正确地添加到sql数据库中。。。所以我的问题在于如何填充我的列表 下面显示的代码是填充前面显示的列表ctrl的函数。。。我似乎找不出哪里出了错。。。有什么想法吗 用于重新启动列表的代码 def

因此,我设法以给定的顺序获得项目列表,以便正确填充数据库。转换项目以填充显示订单中包含的项目的listctrl小部件时。。。。。。。我明白了:

左侧窗口显示添加第一项时发生的情况。。。。右窗是我的问题所在。它实质上覆盖了第一项的文本,并添加了一个空白列。。。。但是,价格加起来是正确的,所有项目都正确地添加到sql数据库中。。。所以我的问题在于如何填充我的列表

下面显示的代码是填充前面显示的列表ctrl的函数。。。我似乎找不出哪里出了错。。。有什么想法吗

用于重新启动列表的代码

    def refresh_list2(self, event):
          """Place the names of each ItemEntry into the list"""

        index = 0
        self.entrydict = {}
        self.list2.DeleteAllItems()
        for entry in self.ordite.list_entries():
                if entry.Order_ID == self.entry.Order_ID:
                   self.list2.InsertStringItem(index,self.entry.Order_ID)
                   self.list2.SetStringItem(index,1,self.entryit.Item_ID)
                   self.list2.SetStringItem(index,2,str(self.entryit.Item_Price))
                   self.list2.SetStringItem(index,3,str(self.entryit.Item_Qty))
                   self.entrydict[index] = entry
                   index += 1
用于将项目插入数据库的代码

    def add_item(self, event):
        """Add a new entry to the Order_Date ite"""
        # start out with blank, generic Entry
        self.entryit = oi.OrderItemEntry(self.entry.Order_ID,"","","0")
        self.endiag = AddItemOrder(self,self.entryit)
        self.endiag.ShowModal()           


        self.ordite.add_entry(self.entry.Order_ID,
                            str(self.entryit.Item_ID),
                            str(self.entryit.Item_Price),
                            str(self.entryit.Item_Qty) )

        self.refresh_list2(None)
        self.addPrice(None)

仍在考虑在后端创建一个函数,根据给定的顺序存储项目对象的筛选列表。\u ID

哦,wx.ListCtrl的乐趣

我花了一个多小时与这些人斗争,然后围绕它编写了一个包装类,以使它大大减少痛苦

您的
刷新列表2()
代码有几个问题

首先,
InsertStringItem()
方法将始终在指定的
索引
之前插入项,因此如果要附加到列表,必须将
索引
设置为大于或等于列表中当前项数的任何值-
sys.maxint
工作得非常好

其次,
InsertStringItem()
将返回它插入项目的索引,因此您必须在对
SetStringItem()
的任何后续调用中使用该值

试试这样的

def refresh_list2(self, event):
      """Place the names of each ItemEntry into the list"""

    self.entrydict = {}
    self.list2.DeleteAllItems()

    for entry in self.ordite.list_entries():
        if entry.Order_ID == self.entry.Order_ID:
           index = self.list2.InsertStringItem(sys.maxint, self.entry.Order_ID)
           self.list2.SetStringItem(index, 1, self.entryit.Item_ID)
           self.list2.SetStringItem(index, 2, str(self.entryit.Item_Price))
           self.list2.SetStringItem(index, 3, str(self.entryit.Item_Qty))
           self.entrydict[index] = entry
…这应该可以达到预期的结果,但是如果随后从列表中删除某个项目,您可能会在
self.entrydict
中遇到问题,因为索引值可能会更改


更新

我不知道你在做什么,但下面这个独立的例子对我来说很有用

import sys
import wx

data = (('O0012', '1001', '235.0', '1'),
        ('O0012', '1002', '600.0', '2'))

app = wx.App(redirect=False)
frame = wx.Frame(None)
lc = wx.ListCtrl(frame, style=wx.LC_REPORT)
lc.InsertColumn(0, 'Order ID')
lc.InsertColumn(1, 'Item ID')
lc.InsertColumn(2, 'Item Price')
lc.InsertColumn(3, 'Item Qty')
for a, b, c, d in data:
    index = lc.InsertStringItem(sys.maxint, a)
    lc.SetStringItem(index, 1, b)
    lc.SetStringItem(index, 2, c)
    lc.SetStringItem(index, 3, d)

frame.Show()
app.MainLoop()

更新#2

啊。我想您的意思是插入本地
条目
变量中的项,而不是实例属性

def refresh_list2(self, event):
      """Place the names of each ItemEntry into the list"""

    self.entrydict = {}
    self.list2.DeleteAllItems()

    for entry in self.ordite.list_entries():
        if entry.Order_ID == self.entry.Order_ID:
           index = self.list2.InsertStringItem(sys.maxint, entry.Order_ID)
           self.list2.SetStringItem(index, 1, entry.Item_ID)
           self.list2.SetStringItem(index, 2, str(entry.Item_Price))
           self.list2.SetStringItem(index, 3, str(entry.Item_Qty))
           self.entrydict[index] = entry

显示的代码看起来无法在所示的gui中生成结果。for循环没有使用entry填充列表Ctrl,每次它为条目中的每个项循环时,都使用相同的self.entry和self.entryit变量。好的。。。将其更改为所有都引用entryit,它是存储项目详细信息的对象。。。。同样的事情发生了…@Aya。。。没什么好改变的…@Aya。。。如果我必须删除某个条目,有没有办法清除entrydict字典?@SeanPerez该问题的最佳解决方案取决于您使用entrydict的目的。它将有助于包含访问它的代码的任何部分-目前,我所能看到的是您构建了它,但从未实际使用过它。@SeanPerez看到更新的答案,如果您有任何意见,您能把它们放在这个答案上,而不是问题上吗,否则我不会得到通知。实际上,它是使用sql的大型应用程序的一部分。我插入的数据本质上是来自sql后端的数据。我知道这与我的刷新列表如何插入项目有关,因为它实际上覆盖了第一个列表条目,并为其余条目写入空格。但是所说的数据被正确地插入SQL后端。。。我真的不想发布我所有的代码,因为它是我的refresh_list2函数的本地化内容。。。。。奇怪的是,它在我的其他模块中应用时工作正常,我无法理解为什么我几乎总是使用ObjectListView而不是ListCtrl,因为它的界面更好,而且我认为它更容易与数据库一起使用。