如何在pyGTK中搜索gtk.ListStore并删除元素? [英] How to search through a gtk.ListStore in pyGTK and remove elements?

查看:128
本文介绍了如何在pyGTK中搜索gtk.ListStore并删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(其中store是gtk.ListStore,titer是gtk.TreeIter.文档说如果没有下一行,iter_next()将返回None,因此在应该可以搜索(int, str)ListStore,并删除其int组件与item_id相匹配的一项.

I have the following code (where store is a gtk.ListStore and titer is a gtk.TreeIter. The docs say that if there is no next row, iter_next() will return None, hence the break when that is found. It is supposed to search through the ListStore of (int, str) and remove the one item whose int component matches item_id.

    while True:
        if store.get_path(titer)[0] == item_id:
            store.remove(titer)
            break
        else:
            titer = store.iter_next(titer)
            if titer is None:
                break

但是,如果中间的元素先前已被删除,而不是titer.iter_next()指向下一个有效元素,则它指向无.这意味着,如果具有正确int值的元素位于先前删除的项目之后,则永远不会找到它.是否有正确的方法来搜索gtk.ListStore以删除项目?

However, if an element in the middle has previously been deleted, instead of titer.iter_next() pointing to the next valid element, it points to None. This means that if the element with the right int value is after the previously deleted item, it never gets found. Is there a correct way to search through a gtk.ListStore to remove items?

推荐答案

我注意到的唯一错误是store.get_path(titer)[0],它将仅获取列表模型的行号.应该是store.get_value(titer, 0).

The only mistake I notice is store.get_path(titer)[0], which will just get the row number of the list model. It should be store.get_value(titer, 0).

顺便说一句,您可以使用(仅限PyGTK) TreeModelRow :

By the way, your code can be expressed in a simpler style using the (PyGTK-only) TreeModelRow:

for row in store:
    if row[0] == item_id:
        store.remove(row.iter)
        break

这篇关于如何在pyGTK中搜索gtk.ListStore并删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆