TypeError:“dict_values”对象不支持从组合框PyqGisPlugin调用层时建立索引

2024-10-01 09:23:56 发布

您现在位置:Python中文网/ 问答频道 /正文

当我从组合框调用层时,抛出这样的错误。 执行Python代码时出错:

TypeError:“dict_values”对象不支持索引 回溯(最近一次呼叫最后一次): selectedLayer=层[selectedLayerIndex1]。层() TypeError:“dict_values”对象不支持索引

Python版本:3.7.0(v3.7.0:1bf9cc5093,2018年6月27日,04:59:51)[MSC v.1914 64位(AMD64)] QGIS版本:3.4.15-Madeira Madeira,e83d02e274

代码 SelectedLayerIndex 1=self.dlg.comboBox.currentIndex()

selectedLayer=层[selectedLayerIndex1]。层()

qgis版本:qgis版本:3.4.15-Madeira Madeir


Tags: 对象代码版本错误dictamd64valuesqgis
2条回答

如果要使用索引,必须首先迭代层并将其添加到LayerList中。只有在thar之后,您才能使用索引并从列表中选择图层(使用索引)

在defrun(self)中使用此代码:

layers_list = []
curLayers = QgsProject.instance().mapLayers().values()
for layer in curLayers:
    layers_list.append(layer)


def chooseIndex():
    sLayerIndex = self.dockwidget.comboBox.currentIndex()
    selectedLayer = layers_list[sLayerIndex]
    print(selectedLayer)

self.dockwidget.comboBox.currentIndexChanged.connect(chooseIndex)

我不确定具体的问题是什么,但经过一些研究,在Python 3中,对字典调用'values()'方法似乎不会返回列表,而是返回'dict_values'对象。
所说的“dict_values”对象不支持索引访问,请查看其文档以了解更多信息

相关问题 更多 >