【问题标题】:Passing external parameter keeping implicit parameter传递外部参数保留隐式参数
【发布时间】:2019-02-20 09:12:53
【问题描述】:

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

基本上,当用户单击它时,我会尝试获取一个功能。

mapTool.featureIdentified.connect(onFeatureIdentified)

所以在函数onfeatureIdentified

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

方法 featureIdentified 传递一个隐式参数。

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参数就会覆盖native方法的隐式参数。

我该怎么办?

【问题讨论】:

  • 试试mapTool.featureIdentified.connect(lambda f: onFeatureIdentified(f, window))
  • 哇,它似乎有效。我觉得很愚蠢,因为我将 lambda 用于其他功能而我忽略了它会起作用。谢谢!

标签: python function tkinter parameter-passing qgis


【解决方案1】:

有两种方法:

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

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

    然后

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

    在您的类的 __init__ 函数中定义您的窗口,然后始终通过 self.window

    引用该窗口
    mapTool.featureIdentified.connect(self.onFeatureIdentified)
    

    然后

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

    第一个参数是self,然后传递函数的native参数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多