pyqtgraph:对于 。items仅显示第二个项目

问题描述 投票:0回答:1

我可以重现pyqtgraph示例GLLinPlotItem.py中的问题。我刚刚添加了行

print (w.items)     

for item in w.items:
    if (type (item) == gl.GLLinePlotItem): 
        print (item)
        w.removeItem (item)

在for循环之后,使用GLLinePlotItems加载绘图。

同时打印(带有项目)将打印我的for循环打印的所有项目,并且仅每秒钟删除一次。

我找到了该线程:Python for loops only every second Item,但没有解决方案。

有什么想法吗?

非常感谢

马丁

编辑:如果您评论w.removeItem,它将打印所有项目。

python for-loop pyqtgraph
1个回答
0
投票

[好吧,我发现自己:removeItem更改列表,将每个后续项目向前移动一个索引,而循环索引保持不变。因此,对于in循环,内部仅循环通过索引。我认为当列表更改时,它们会考虑在内。再次学到新的东西:-)因此,我的解决方案是从背面浏览列表,因此删除项目不会影响索引,因为它从正面开始计数:

itm_cnt = len (w.items)
idx = itm_cnt - 1
print (itm_cnt)    
while (idx >= 0):
    if (type (w.items [idx]) == gl.GLLinePlotItem): 
        print (idx, w.items [idx])
#        w.removeItem (w.items [idx])
        del w.items [idx]
    idx -= 1

我希望这对其他人有帮助。马丁

© www.soinside.com 2019 - 2024. All rights reserved.