python tkinter canvas 显示图片的示例


Posted in Python onJune 13, 2019

先来看一下该方法的说明

create_image(position, **options) [#]
Draws an image on the canvas.

position
Image position, given as two coordinates.
**options
Image options.
activeimage=
anchor=
Where to place the image relative to the given position. Default is CENTER.
disabledimage=
image=
The image object. This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage). The application must keep a reference to the image object.
state=
Item state. One of NORMAL, DISABLED, or HIDDEN.
tags=
A tag to attach to this item, or a tuple containing multiple tags.
Returns:
The item id.

关于image有两个重要的点要注意,一个是格式,第二是要保持持续引用

The image object. This should be a

1.This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage).

2.The application must keep a reference to the image object.

因此代码应该这样写,并且变量im应该是全局变量

image = Image.open("img.jpg") 
im = ImageTk.PhotoImage(image) 

canvas.create_image(300,50,image = im)

但如果我就是想要在方法里调用怎么办?

那么可以提前声明全局变量

image = None
im = None

之后在方法里使用global来声明变量为全局变量

即:

def method():
  global image
  global im
  image = Image.open("img.jpg") 
  im = ImageTk.PhotoImage(image) 
  ...

以上这篇python tkinter canvas 显示图片的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python和Ruby中each循环引用变量问题(一个隐秘BUG?)
Jun 04 Python
Python入门之后再看点什么好?
Mar 05 Python
python 堆和优先队列的使用详解
Mar 05 Python
Django Channels 实现点对点实时聊天和消息推送功能
Jul 17 Python
处理python中多线程与多进程中的数据共享问题
Jul 28 Python
Pandas DataFrame中的tuple元素遍历的实现
Oct 23 Python
PyCharm+Pipenv虚拟环境开发和依赖管理的教程详解
Apr 16 Python
Python使用struct处理二进制(pack和unpack用法)
Nov 12 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 Python
python中time tzset()函数实例用法
Feb 18 Python
python实现学生通讯录管理系统
Feb 25 Python
简单且有用的Python数据分析和机器学习代码
Jul 02 Python
Python通用函数实现数组计算的方法
Jun 13 #Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
Jun 13 #Python
Pycharm如何打断点的方法步骤
Jun 13 #Python
用pycharm开发django项目示例代码
Jun 13 #Python
pyqt5 实现工具栏文字图片同时显示
Jun 13 #Python
Python自动化运维之Ansible定义主机与组规则操作详解
Jun 13 #Python
pyqt 实现在Widgets中显示图片和文字的方法
Jun 13 #Python
You might like
解析php中如何直接执行SHELL
2013/06/28 PHP
Yii2.0多文件上传实例说明
2017/07/24 PHP
laravel 数据验证规则详解
2019/10/23 PHP
jQuery validate 中文API 附validate.js中文api手册
2010/07/31 Javascript
基于PHP+Jquery制作的可编辑的表格的代码
2011/04/10 Javascript
jQuery UI AutoComplete 使用说明
2011/06/20 Javascript
jquery表单验证使用插件formValidator
2012/11/10 Javascript
JS+CSS实现感应鼠标渐变显示DIV层的方法
2015/02/20 Javascript
深入浅析knockout源码分析之订阅
2016/07/12 Javascript
nodejs实例解析(输出hello world)
2017/01/03 NodeJs
浅析node.js的模块加载机制
2018/05/25 Javascript
Angular路由ui-router配置详解
2018/08/01 Javascript
使用Easyui实现查询条件的后端传递并自动刷新表格的两种方法
2019/09/09 Javascript
UEditor 自定义图片视频尺寸校验功能的实现代码
2020/10/20 Javascript
[35:29]Secret vs VG 2018国际邀请赛淘汰赛BO3 第三场 8.23
2018/08/24 DOTA
python实现复制整个目录的方法
2015/05/12 Python
python使用reportlab实现图片转换成pdf的方法
2015/05/22 Python
Mac 上切换Python多版本
2017/06/17 Python
django限制匿名用户访问及重定向的方法实例
2018/02/07 Python
在python win系统下 打开TXT文件的实例
2018/04/29 Python
python输入整条数据分割存入数组的方法
2018/11/13 Python
Python设计模式之备忘录模式原理与用法详解
2019/01/15 Python
详解python中@的用法
2019/03/27 Python
Python面向对象思想与应用入门教程【类与对象】
2019/04/12 Python
Django分页功能的实现代码详解
2019/07/29 Python
python中利用matplotlib读取灰度图的例子
2019/12/07 Python
Python编程快速上手——选择性拷贝操作案例分析
2020/02/28 Python
如何搭建pytorch环境的方法步骤
2020/05/06 Python
html5中监听canvas内部元素点击事件的三种方法
2019/04/28 HTML / CSS
html5利用canvas实现颜色容差抠图功能
2019/12/23 HTML / CSS
Hunter Boots美国官方网站:赫特威灵顿雨靴
2018/06/16 全球购物
简述DNS进行域名解析的过程
2013/12/02 面试题
楼面部长岗位职责范本
2014/02/14 职场文书
幼儿园小班教师寄语
2014/04/03 职场文书
学校文明单位申报材料
2014/05/06 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书