wxPython listctrl insertitem和SetItem一次全部

时间:2019-03-15 00:54:11

标签: python-3.x wxpython-phoenix

我有一个listctrl,

    self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT | wx.LC_NO_HEADER)
    self.list.InsertColumn(col=0, heading='', format=wx.LIST_FORMAT_CENTER, width=150)
    self.list.InsertColumn(col=1, heading='', format=wx.LIST_FORMAT_CENTER, width=450)

    for person in people:
        #this is the issue right here
        index = self.list.InsertItem(0, person.age) #one line to insert an item and get the index, and the next line to update the item at that index... two lines to actually put a full entry in.  
        self.list.SetItem(index=index, column=1, label=person.name)

最初在构造函数中设置listctrl可以很好地工作,但是如果我想在运行时动态添加/删除listctrl中的项目怎么办?

我遇到过wx.CallAfter,wx.CallLater,startWorker(来自wx.lib.delayedresult)和wx.Timer

如果您看上面的示例,问题是我有一行插入项目,另一行将项目更新为在刚插入的项目上具有正确的名称。因此,如果我的线程轮流删除并向listctrl添加项目,如果我插入一个项目,而另一个线程同时插入一个项目,那么我刚得到的索引与更新无关。即我需要一个原子操作来插入一个项目,包括同时插入该人的年龄和该人的姓名。所以我的第一个问题是,有没有一种方法可以将列表项的所有信息插入一行?

如果我不能这样做,那么我的下一个问题是如何完成规定的行为?例如,假设有多个线程随机对第一行进行着色,添加和删除:

    self.color_timer = wx.Timer(self)
    self.Bind(wx.EVT_TIMER, self.item_colorizer, self.color_timer)
    self.red_shown = True
    self.color_timer.Start(500)

    self.delete_timer = wx.Timer(self)
    self.Bind(wx.EVT_TIMER, self.item_deleter, self.delete_timer)
    self.delete_timer.Start(500)


    self.adder_timer = wx.Timer(self)
    self.Bind(wx.EVT_TIMER, self.item_queuer, self.adder_timer)
    self.adder_timer.Start(400)

然后这是我用来添加人员,删除人员并为第一行着色的方法:

def item_queuer(self, event):
    startWorker(consumer=self.item_adder,
                    workerFn=self.person_generator)


def person_generator(self):
    return {'person':random.choice(people)}

def item_adder(self, result):
    res = result.get()
    person = res['person']
    wx.CallAfter(self.list.InsertItem, 0, person.name) # this is the problem right here.  If another thread does a color swap in between this line and the next, then this won't work. 
    wx.CallAfter(self.list.SetItem, index=0, column=1, label=person.name)

def item_deleter(self, event):
    wx.CallAfter(self.list.DeleteItem, 0)

def item_colorizer(self, event):
    if self.color_timer.IsRunning():
        if not self.red_shown:
            wx.CallAfter(self.list.SetItemBackgroundColour, 0, wx.RED)
            wx.CallAfter(self.list.SetItemTextColour, 0, wx.WHITE)
            self.red_shown = True
        else:
            wx.CallAfter(self.list.SetItemBackgroundColour, 0, wx.BLUE)
            wx.CallAfter(self.list.SetItemTextColour, 0, wx.BLACK)
            self.red_shown = False

执行此操作时实际发生的事情是,我最终在一行中插入了部分人物(只是年龄),并且颜色在插入名称之前发生了变化。我注意到listctrl上的InsertItem方法已重载,并提供了一个签名,可以在其中插入ListItem,但我也无法使它正常工作。

    item1 = wx.ListItem()
    item1.SetBackgroundColour(wx.GREEN)
    item1.SetColumn(0)
    item1.SetText(32)
    item1.SetColumn(1)
    item1.SetText('John')
    self.list.InsertItem(item1)

wx._core.wxAssertionError:C ++断言“ rv!= -1”在.... \ wxListCtrl :: InsertItem()中的.... src \ msw \ listctrl.cpp(1730)失败:无法在wxListCtrl中插入项目

0 个答案:

没有答案
相关问题