详解python tkinter 图片插入问题


Posted in Python onSeptember 03, 2020

通过tkinter.PhotoImage插入GIF, PGM/PPM格式的图片。

import tkinter

class Gui:
  def __init__(self):  
    self.gui=tkinter.Tk()                        # create gui window
    self.gui.title("Image Display")                   # set the title of gui 
    self.gui.geometry("800x600")                    # set the window size of gui 

    img = tkinter.PhotoImage(file="C:/Users/15025/Desktop/bear.gif")  # read image from path

    label1=tkinter.Label(self.gui,image=img)              # create a label to insert this image
    label1.grid()                            # set the label in the main window
 
    self.gui.mainloop()                         # start mainloop

main = Gui()

注意: img = tkinter.PhotoImage(file="C:/Users/15025/Desktop/bear.gif") 中的关键字file不能够省略,否则程序无法正常显示图片。

对于常用的PNG,与JPG格式的图片,我们需要从python image library(pillow)(PIL)导入Image与ImageTk模块来实现,代码如下:

import tkinter
from PIL import Image
from PIL import ImageTk


class Gui:
  def __init__(self):  
    self.gui=tkinter.Tk()                # create gui window
    self.gui.title("Image Display")           # set the title of gui 
    self.gui.geometry("800x600")            # set the window size of gui 

    load = Image.open("C:/Users/15025/Desktop/1.png")  # open image from path
    img = ImageTk.PhotoImage(load)           # read opened image

    label1=tkinter.Label(self.gui,image=img)      # create a label to insert this image
    label1.grid()                    # set the label in the main window
 
    self.gui.mainloop()                 # start mainloop

main = Gui()

然而在实际操作中,本人使用的是Anaconda spyder编译器,当我们在读入图像时程序出错后,再次运行程序就会导致image "pyimage1" doesn't exist错误,每次运行一次,数字就会增加1,如:image "pyimage2" doesn't exist。遇到此错误,可以直接在IPython控制台界面重启IPython内核即可,或者关闭编译器并重新打开。

看似我们已经完全实现了图片的插入,但是这种插入方法是存在隐患的,具体代码如下:

import tkinter as tk
from PIL import Image
from PIL import ImageTk


class Gui(tk.Tk):
  def __init__(self):
    super().__init__()
    self.title("Figure dynamic show v1.01")
    # self.geometry("1000x800+400+100")
    self.mainGui()
    # self.mainloop()
    

  def mainGui(self):
    image = Image.open("C:/Users/15025/Desktop/1.png")
    photo = ImageTk.PhotoImage(image)
    label = tk.Label(self, image=photo)
    label.image = photo     # in case the image is recycled
    label.grid()
    

main = Gui()
main.mainloop()

这里我们可以看到相比较上面的程序,我们将Gui界面的图像插入部分分离到另一个函数中,并且直接定义一个tkinter的类,这样做的好处是我们可以直接用self替代创建的主窗口界面,并且我们可以在不同的地方启动主循环,self.mainloop()和main.mainloop()任选一个即可。并且因为我们想要插入图片,所以我们可以省略指定Gui界面的尺寸,这样做的好处是会创建一个自适应图片大小的Gui界面。最重要的是我们可以看到多了一行代码label.image = photo,我们将选取的图片photo赋值给了label的属性对象image,如果没有这一行代码,图片便无法正常显示,这是因为python会自动回收不使用的对象,所以我们需要使用属性对象进行声明。 上述的程序隐患便是因为缺少了这一行代码。

至此,tkinter的图片插入可暂时告一段落。

到此这篇关于详解python tkinter 图片插入问题的文章就介绍到这了,更多相关python tkinter 图片插入内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python进阶教程之异常处理
Aug 30 Python
在Linux系统上安装Python的Scrapy框架的教程
Jun 11 Python
Python每天必学之bytes字节
Jan 28 Python
python3.6+opencv3.4实现鼠标交互查看图片像素
Feb 26 Python
python opencv 图像尺寸变换方法
Apr 02 Python
Python解析、提取url关键字的实例详解
Dec 17 Python
python用for循环求和的方法总结
Jul 08 Python
解决python中导入win32com.client出错的问题
Jul 26 Python
Python with语句和过程抽取思想
Dec 23 Python
Python爬虫程序架构和运行流程原理解析
Mar 09 Python
Python3 如何开启自带http服务
May 18 Python
Python基础数据类型tuple元组的概念与用法
Aug 02 Python
解决PyCharm IDE环境下,执行unittest不生成测试报告的问题
Sep 03 #Python
PyTorch中Tensor的数据类型和运算的使用
Sep 03 #Python
python开发入门——set的使用
Sep 03 #Python
使用anaconda安装pytorch的实现步骤
Sep 03 #Python
解决Python安装cryptography报错问题
Sep 03 #Python
解决python打开https出现certificate verify failed的问题
Sep 03 #Python
详解torch.Tensor的4种乘法
Sep 03 #Python
You might like
php控制linux服务器常用功能 关机 重启 开新站点等
2012/09/05 PHP
PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
2014/05/10 PHP
php实现的一个简单json rpc框架实例
2015/03/30 PHP
CodeIgniter框架常见用法工作总结
2017/03/16 PHP
Alliance vs AM BO3 第二场2.13
2021/03/10 DOTA
一些有关检查数据的JS代码
2006/09/07 Javascript
js里怎么取select标签里的值并修改
2012/12/10 Javascript
js判断选择时间不能小于当前时间的示例代码
2013/09/24 Javascript
JavaScript中的fontsize()方法使用详解
2015/06/08 Javascript
node.js实现回调的方法示例
2017/03/01 Javascript
vue+vue-validator 表单验证功能的实现代码
2017/11/13 Javascript
在Vue中使用Compass的方法
2018/03/02 Javascript
JS复杂判断的更优雅写法代码详解
2018/11/07 Javascript
Ant Design Vue 添加区分中英文的长度校验功能
2020/01/21 Javascript
微信小程序中的列表切换功能实例代码详解
2020/06/09 Javascript
vue中组件通信详解(父子组件, 爷孙组件, 兄弟组件)
2020/07/27 Javascript
ant-design-vue 时间选择器赋值默认时间的操作
2020/10/27 Javascript
Vue router安装及使用方法解析
2020/12/02 Vue.js
Python找出文件中使用率最高的汉字实例详解
2015/06/03 Python
python实现的希尔排序算法实例
2015/07/01 Python
Python for Informatics 第11章之正则表达式(二)
2016/04/21 Python
Django中url的反向查询的方法
2018/03/14 Python
使用Python机器学习降低静态日志噪声
2018/09/29 Python
python中 * 的用法详解
2019/07/10 Python
Python操作SQLite数据库过程解析
2019/09/02 Python
Python SSL证书验证问题解决方案
2020/01/13 Python
详解python 破解网站反爬虫的两种简单方法
2020/02/09 Python
法学专业本科生自荐信范文
2013/12/17 职场文书
党员培训思想汇报
2014/01/07 职场文书
试用期员工考核制度
2014/01/22 职场文书
网上蛋糕店创业计划书
2014/01/24 职场文书
讲解员培训方案
2014/05/04 职场文书
集团财务总监岗位职责
2015/04/03 职场文书
社区敬老月活动总结
2015/05/07 职场文书
大学生受助感言
2015/08/01 职场文书
Java使用JMeter进行高并发测试
2021/11/23 Java/Android