pyqgis iface AttributeError: ‘NoneType‘ object has no attribute ‘mapCanvas‘
从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,开发手册中没有看到对库的分类,以为都可以用呢。
参考链接:
- https://gis.stackexchange.com/questions/363363/difference-between-iface-mapcanvas-and-qgsmapcanvas
- https://courses.spatialthoughts.com/pyqgis-in-a-day.html
- https://subscription.packtpub.com/book/application-development/9781787124837/1/ch01lvl1sec19/building-a-standalone-application
pyqgis iface AttributeError: ‘NoneType‘ object has no attribute ‘mapCanvas‘相关推荐
- 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 ...
- Keras问题“AttributeError: 'NoneType' object has no attribute 'update”解决
BUG 在使用Keras训练模型时,在每个epoch完成后save_model时会报错 "AttributeError: 'NoneType' object has no attribute ...
- pyspark AttributeError: 'NoneType' object has no attribute 'setCallSite'
pyspark: AttributeError: 'NoneType' object has no attribute 'setCallSite' 我草,是pyspark的bug.解决方法: prin ...
- Pywinauto 应用后端类型选择错误:AttributeError: ‘NoneType‘ object has no attribute ‘backend‘. 原因及解决办法
AttributeError: 'NoneType' object has no attribute 'backend'. 错误原因: 选择的应用后端类型不对. windows 上应用的后端类型有两种 ...
- 成功解决AttributeError: ‘NoneType‘ object has no attribute ‘shape‘
成功解决AttributeError: 'NoneType' object has no attribute 'shape' 目录 解决问题 解决思路 解决方法 解决问题 multiplier = [ ...
- 成功解决AttributeError: 'NoneType' object has no attribute '__array_interface__'
成功解决AttributeError: 'NoneType' object has no attribute '__array_interface__' 目录 解决问题 解决思路 解决方法 原因及思路 ...
- AttributeError: 'NoneType' object has no attribute 'grid'报错解决方案
1问题描述: 当我们在使用tkinter时经常遇到AttributeError: 'NoneType' object has no attribute 'grid'的报错 2.原因分析: import ...
- Python学习笔记:‘’AttributeError: NoneType object has no attribute‘’
前言 最近在学习python,犯了很多低级错误,总结一下 目录 文章目录 前言 目录 问题 出处 方案 问题 AttributeError: 'NoneType' object has no attr ...
- 解决AttributeError AttributeError: 'NoneType' object has no attribute 'filename'
原因忘记上传文件 表单需要加属性 enctype="multipart/form-data" 否则报错!AttributeError AttributeError: 'NoneTy ...
- Scrapy爬虫报错AttributeError: ‘NoneType‘ object has no attribute ‘write‘
前言 一.报错 AttributeError: 'NoneType' object has no attribute 'write' 二.报错原因 1.piplines文件中的方法不能自定义的呢 开始 ...
最新文章
- SAP Retail MM41 维护商品主数据,报错 - 估价范围 NM01 还没有生产式的物料帐簿 – 之对策
- linux从源码编译软件,linux软件包管理——源码包编译安装
- 命令行模式下的后向引用替换
- 一文带你理解云原生|云原生全景指南
- [BZOJ4182]Shopping
- 论文集 | 精选133篇知识图谱论文
- 编程语言之问:何时该借用,何时该创造?
- docker+mysql+授权_docker安装mysql, 授权远程连接
- 前端工程化之FaaS SSR方案​
- 单片机实验(十二)单片机矩阵按键控制数码管
- CC3200学习——blinky资料
- new Date()时间不是当前时间问题的解决方法
- 1155 服务器芯片组,2014年主流主板芯片组分析
- 在线制作banner
- 【设计模式】建造者模式
- linux文件名带星号,Linux安装之bash星号用法
- Linux中系统的分区管理
- 【解决问题】413错误 413 Request Entity Too Large 接口返回413 报413nginx
- 一本通 P1486 【黑暗城堡】
- Free-Form Image Inpainting with Gated Convolution
热门文章
- 启动kafka时报错:java.nio.file.FileSystemException,另一个程序正在使用此文件,进程无法访问。
- Mac添加Windows共享打印机
- PCI/PCIe硬件相关知识
- 牛客网算法之洗牌详解
- Spring 5.0.3.RELEASE中的 Kotlin 语言等支持
- php 的一元二次方程_php扩展 求解一元二次方程
- 基于Springboot+Mybatis+Mysql的人事考勤统计管理系统
- QQ省市县三级联动JS
- 智慧城市运营典型模式特征分析
- 4. HTML 视频