传递外部参数保持隐式参数

2024-10-02 02:29:30 发布

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

我正在为qgis 3编写一个python插件

基本上,当用户点击一个特性时,我会尝试获取它

mapTool.featureIdentified.connect(onFeatureIdentified)

因此,在函数中确定的特性

def onFeatureIdentified(feature):
        print("feature selected : "+ str(feature.id()))

已识别的方法功能传递隐式参数

void QgsMapToolIdentifyFeature::featureIdentified ( const QgsFeature & ) void QgsMapToolIdentifyFeature::featureIdentified ( QgsFeatureId )

我的问题是,我想传递一个其他参数的功能(我想关闭我的窗口时,一个功能是确定的) 就像这样:

mapTool.featureIdentified.connect(onFeatureIdentified(window))

def onFeatureIdentified(feature,window):
            print("feature selected : "+ str(feature.id()))
            window.quit()

这样,window参数就会覆盖本机方法的隐式参数

我该怎么办


Tags: 方法功能id参数defconnect特性window
1条回答
网友
1楼 · 发布于 2024-10-02 02:29:30

有两种方法:

  • 使用lambda函数(归功于acw1668)传递第二个参数

    mapTool.featureIdentified.connect(lambda feature: onFeatureIdentified(feature, window))
    

    那么

    def onFeatureIdentified(feature,window):
    
  • 如果你使用class(像我一样):

    在类的\uu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

    mapTool.featureIdentified.connect(self.onFeatureIdentified)
    

    那么

    def onFeatureIdentified(self,feature):
    print("feature selected : "+ str(feature.id()))
    self.window.quit()
    

    第一个参数将是self,然后它将传递函数的本机参数

相关问题 更多 >

    热门问题