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计算程序开始到程序结束的运行时间和程序运行的CPU时间
Nov 28 Python
Python日志模块logging简介
Apr 13 Python
Python 正则表达式入门(中级篇)
Dec 07 Python
对python中Matplotlib的坐标轴的坐标区间的设定实例讲解
May 25 Python
python调用staf自动化框架的方法
Dec 26 Python
用python生成与调用cntk模型代码演示方法
Aug 26 Python
学习Django知识点分享
Sep 11 Python
在 Python 中使用 MQTT的方法
Aug 18 Python
python os.rename实例用法详解
Dec 06 Python
django注册用邮箱发送验证码的实现
Apr 18 Python
python办公自动化之excel的操作
May 23 Python
用Python编写简单的gRPC服务的详细过程
Jul 04 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新手上路(四)
2006/10/09 PHP
PHP 字符串加密函数(在指定时间内加密还原字符串,超时无法还原)
2010/04/28 PHP
检测png图片是否完整的php代码
2010/09/06 PHP
解析PHP的session过期设置
2013/06/29 PHP
ThinkPHP中ajax使用实例教程
2014/08/22 PHP
php操作csv文件代码实例汇总
2014/09/22 PHP
WordPress中限制非管理员用户在文章后只能评论一次
2015/12/31 PHP
PHP中数组转换为SimpleXML教程
2019/01/27 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
jquery实现html页面 div 假分页有原理有代码
2014/09/06 Javascript
在Node.js应用中读写Redis数据库的简单方法
2015/06/30 Javascript
轻松学习jQuery插件EasyUI EasyUI实现树形网络基本操作(2)
2015/11/30 Javascript
JS简单编号生成器实现方法(附demo源码下载)
2016/04/05 Javascript
angular4模块中给标签添加背景图的实现方法
2017/09/15 Javascript
IE11下使用canvas.toDataURL报SecurityError错误的解决方法
2017/11/19 Javascript
Vue CLI3 如何支持less的方法示例
2018/08/29 Javascript
vue使用better-scroll实现滑动以及左右联动
2020/06/30 Javascript
[01:04:05]VG vs Newbee 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
python 正则式使用心得
2009/05/07 Python
python实现zencart产品数据导入到magento(python导入数据)
2014/04/03 Python
python计算圆周率pi的方法
2015/07/11 Python
基于Python实现的微信好友数据分析
2018/02/26 Python
在Python中调用Ping命令,批量IP的方法
2019/01/26 Python
ubuntu 安装pyqt5和卸载pyQt5的方法
2020/03/24 Python
Python Django搭建网站流程图解
2020/06/13 Python
环境科学毕业生自荐信
2013/11/21 职场文书
中学教师培训制度
2014/01/31 职场文书
《莫高窟》教学反思
2014/02/25 职场文书
课前三分钟演讲稿
2014/04/24 职场文书
禁毒宣传活动总结
2014/08/26 职场文书
飞越疯人院观后感
2015/06/09 职场文书
社区宣传标语口号
2015/12/26 职场文书
《植物妈妈有办法》教学反思
2016/02/23 职场文书
MySQL REVOKE实现删除用户权限
2021/06/18 MySQL
Netflix《海贼王》真人版剧集多张片场照曝光
2022/04/04 日漫
mysql使用FIND_IN_SET和group_concat两个方法查询上下级机构
2022/04/20 MySQL