从qgis获取mapCanvas报错,“AttributeError: ‘NoneType’ object has no attribute ‘mapCanvas’”

from qgis.utils import ifacevlayer = QgsVectorLayer("data/layers/layer1.gpkg", "layer_name", "ogr")
canvas= iface.mapCanvas()
canvas.setExtent(layer.extent())
canvas.refresh()

但是改为下面的代码就可以:

ex = vlayer.extent()
canvas = QgsMapCanvas()
canvas.setExtent(ex)

据参考1里的解释,QgsMapCanvas是一个单例模式的类,可以随时调用获取单例。iface.mapCanvas也实现了这个,可以获取mapCanvas单例,但是需要QGIS的应用包括GUI,就是有界面。
而standalone的基于qgis的独立的应用程序,可能有界面,也可能没有界面,没有默认的canvas。
所以独立程序初始化的时候如果没给主程序一个mapcanvas,相当于iface没有初始化,对象就是空的。所以报错nonetype 对象。

实际上, iface是在嵌入的脚本程序里面用到的,standalone。自定义的程序custom application里面不能直接用。因为对pyqgis的库不熟悉,也没用过桌面版的QGIS,开发手册中没有看到对库的分类,以为都可以用呢。

参考链接:

  1. https://gis.stackexchange.com/questions/363363/difference-between-iface-mapcanvas-and-qgsmapcanvas
  2. https://courses.spatialthoughts.com/pyqgis-in-a-day.html
  3. https://subscription.packtpub.com/book/application-development/9781787124837/1/ch01lvl1sec19/building-a-standalone-application

pyqgis iface AttributeError: ‘NoneType‘ object has no attribute ‘mapCanvas‘相关推荐

  1. Traceback (most recent call last): File AttributeError: 'NoneType' object has no attribute 'group'

    Traceback (most recent call last):File "<stdin>", line 1, in <module> Attribut ...

  2. Keras问题“AttributeError: 'NoneType' object has no attribute 'update”解决

    BUG 在使用Keras训练模型时,在每个epoch完成后save_model时会报错 "AttributeError: 'NoneType' object has no attribute ...

  3. pyspark AttributeError: 'NoneType' object has no attribute 'setCallSite'

    pyspark: AttributeError: 'NoneType' object has no attribute 'setCallSite' 我草,是pyspark的bug.解决方法: prin ...

  4. Pywinauto 应用后端类型选择错误:AttributeError: ‘NoneType‘ object has no attribute ‘backend‘. 原因及解决办法

    AttributeError: 'NoneType' object has no attribute 'backend'. 错误原因: 选择的应用后端类型不对. windows 上应用的后端类型有两种 ...

  5. 成功解决AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

    成功解决AttributeError: 'NoneType' object has no attribute 'shape' 目录 解决问题 解决思路 解决方法 解决问题 multiplier = [ ...

  6. 成功解决AttributeError: 'NoneType' object has no attribute '__array_interface__'

    成功解决AttributeError: 'NoneType' object has no attribute '__array_interface__' 目录 解决问题 解决思路 解决方法 原因及思路 ...

  7. AttributeError: 'NoneType' object has no attribute 'grid'报错解决方案

    1问题描述: 当我们在使用tkinter时经常遇到AttributeError: 'NoneType' object has no attribute 'grid'的报错 2.原因分析: import ...

  8. Python学习笔记:‘’AttributeError: NoneType object has no attribute‘’

    前言 最近在学习python,犯了很多低级错误,总结一下 目录 文章目录 前言 目录 问题 出处 方案 问题 AttributeError: 'NoneType' object has no attr ...

  9. 解决AttributeError AttributeError: 'NoneType' object has no attribute 'filename'

    原因忘记上传文件 表单需要加属性 enctype="multipart/form-data" 否则报错!AttributeError AttributeError: 'NoneTy ...

  10. Scrapy爬虫报错AttributeError: ‘NoneType‘ object has no attribute ‘write‘

    前言 一.报错 AttributeError: 'NoneType' object has no attribute 'write' 二.报错原因 1.piplines文件中的方法不能自定义的呢 开始 ...

最新文章

  1. SAP Retail MM41 维护商品主数据,报错 - 估价范围 NM01 还没有生产式的物料帐簿 – 之对策
  2. linux从源码编译软件,linux软件包管理——源码包编译安装
  3. 命令行模式下的后向引用替换
  4. 一文带你理解云原生|云原生全景指南
  5. [BZOJ4182]Shopping
  6. 论文集 | 精选133篇知识图谱论文
  7. 编程语言之问:何时该借用,何时该创造?
  8. docker+mysql+授权_docker安装mysql, 授权远程连接
  9. 前端工程化之FaaS SSR方案​
  10. 单片机实验(十二)单片机矩阵按键控制数码管
  11. CC3200学习——blinky资料
  12. new Date()时间不是当前时间问题的解决方法
  13. 1155 服务器芯片组,2014年主流主板芯片组分析
  14. 在线制作banner
  15. 【设计模式】建造者模式
  16. linux文件名带星号,Linux安装之bash星号用法
  17. Linux中系统的分区管理
  18. 【解决问题】413错误 413 Request Entity Too Large 接口返回413 报413nginx
  19. 一本通 P1486 【黑暗城堡】
  20. Free-Form Image Inpainting with Gated Convolution

热门文章

  1. 启动kafka时报错:java.nio.file.FileSystemException,另一个程序正在使用此文件,进程无法访问。
  2. Mac添加Windows共享打印机
  3. PCI/PCIe硬件相关知识
  4. 牛客网算法之洗牌详解
  5. Spring 5.0.3.RELEASE中的 Kotlin 语言等支持
  6. php 的一元二次方程_php扩展 求解一元二次方程
  7. 基于Springboot+Mybatis+Mysql的人事考勤统计管理系统
  8. QQ省市县三级联动JS
  9. 智慧城市运营典型模式特征分析
  10. 4. HTML 视频