Skip to main content
 首页 » 编程设计

python之更新 PyGTK 组合框时如何保留选择

2025年05月04日6exmyth

这是 question 的后续行动我昨天问过。我想要做的是获取一组 PyGTK 组合框,以便在其中一个中选择一个值时得到更新。 Initially they all contain the same items to choose from, but when a value is selected this value should no longer be available in any of the other comboboxes (unless it's present as a duplicate in the original list).

我已经创建了一个值列表,该列表应该可用于特定组合框,回答昨天问题的人也很友好地为我提供了一种列出框中所有当前存在项目的方法,所以我现在将“应该可用”列表与“当前可用列表”进行比较,如果这两个列表不同,则只着手更新组合框。一切正常 - 除了以下事实:每当我从组合框中删除旧内容时,所有当前选择都会消失,而我尝试重新建立选择只会导致组合框无限相互更新。

我进行了几次搜索,但我发现的方法都没有对我有用,所以我又无能为力了,因此如果有人能帮助我就太好了。这是我正在使用的代码的当前版本:

# 'combo_list' contains all the comboboxes of interest 
# 'indx_in_combo_list' is the index (from 'combo_list') of the combobox which 
# triggered the updating process 
 
def changed_single_score(self, source_combo, all_scores, combo_list, indx_in_combo_list): 
    # append '0' so that swapping values remains possible 
    available_items = all_scores.split(', ') + ['0'] 
 
    for i in range(len(combo_list)): 
        # make items unavailable that are selected in one of the boxes 
        selected = self.get_active_text(combo_list[i]) 
        if selected in available_items: 
            available_items.remove(selected) 
 
    for indx in range(len(combo_list)): 
        # don't try changing the combobox which triggered updating 
        if indx != indx_in_combo_list: 
            existing_items = [model_item[0] for model_item in combo_list[indx].get_model()] 
            local_selection = self.get_active_text(combo_list[indx]) 
            local_available = available_items[:] 
 
            # each box must retain its own selected item 
            if local_selection != None: 
               local_available.append(local_selection) 
 
            local_available.sort() 
            local_available.reverse() 
            existing_items.sort() 
            existing_items.reverse() 
 
            if existing_items != local_available: 
 
                # if I uncomment the following line the boxes are cleared 
                # but all selections vanish, too 
                #combo_list[indx].get_model().clear() 
 
                for item in local_available: 
                    combo_list[indx].append_text(item) 
 
                # I want to make sure that the selection is retained, 
                # but if I do it like this I just cause emission of the 
                # 'changed' signal, thus I get infinite recursion again 
                #if local_selection != None: 
                #    combo_list[indx].set_active(local_available.index(local_selection)) 

上述函数在以下上下文中调用:

combo_list[indx].connect('changed', self.changed_single_score, self.selected['scores'], combo_list, indx) 

我也试过一个一个地删除所有旧条目,只保留一个与选择,但这也没有用。我可能再次忽略了明显的解决方案,但如果这里有人可以向我指出,那就太好了!

非常感谢。

请您参考如下方法:

您可以在函数顶部使用 bool 值锁定代码,使其不被在您填充组合框时调用它的其他信号执行。

if self.changed_single_score_locked: 
    return 
else: 
    self.changed_single_score_locked = True 
  1. 存储选择的内容
  2. 清除框
  3. 重新填充
  4. 选择已选择的内容

然后在函数末尾解除锁:

self.changed_single_score_locked = False 

或者使用 GTK API 通过其他方式完全关闭信号?

编辑:改为使用 handler_block。