详解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使用htpasswd实现基本认证授权的例子
Jun 10 Python
Python 常用string函数详解
May 30 Python
简单了解什么是神经网络
Dec 23 Python
教你用一行Python代码实现并行任务(附代码)
Feb 02 Python
Python实现查看系统启动项功能示例
May 10 Python
NLTK 3.2.4 环境搭建教程
Sep 19 Python
python 执行文件时额外参数获取的实例
Dec 18 Python
Anaconda之conda常用命令介绍(安装、更新、删除)
Oct 06 Python
安装完Python包然后找不到模块的解决步骤
Feb 13 Python
Pycharm2020.1安装无法启动问题即设置中文插件的方法
Aug 07 Python
python 基于opencv操作摄像头
Dec 24 Python
解决python绘图使用subplots出现标题重叠的问题
Apr 30 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通过header实现文本文件下载的代码
2010/08/08 PHP
php中限制ip段访问、禁止ip提交表单的代码分享
2014/08/22 PHP
php7函数,声明,返回值等新特性介绍
2018/05/25 PHP
php中html_entity_decode实现HTML实体转义
2018/06/13 PHP
PHP基于openssl实现的非对称加密操作示例
2019/01/11 PHP
深入理解PHP+Mysql分布式事务与解决方案
2020/12/03 PHP
javascript 字符 Escape,encodeURI,encodeURIComponent
2009/07/09 Javascript
JavaScript 利用Cookie记录用户登录信息
2009/12/08 Javascript
JavaScript Memoization 让函数也有记忆功能
2011/10/27 Javascript
javascript如何创建表格(javascript绘制表格的二种方法)
2013/12/10 Javascript
JS小游戏之宇宙战机源码详解
2014/09/25 Javascript
Node.js 条形码识别程序构建思路详解
2016/02/14 Javascript
js下将金额数字每三位一逗号分隔
2016/02/19 Javascript
jquery表格datatables实例解析 直接加载和延迟加载
2016/08/12 Javascript
js实现自定义路由
2017/02/04 Javascript
javascript事件的绑定基础实例讲解(34)
2017/02/14 Javascript
详谈jQuery中使用attr(), prop(), val()获取value的异同
2017/04/25 jQuery
react+ant design实现Table的增、删、改的示例代码
2018/12/27 Javascript
JS实现瀑布流效果
2020/03/07 Javascript
谈谈JavaScript中的垃圾回收机制
2020/09/17 Javascript
Python3 读、写Excel文件的操作方法
2018/10/20 Python
python保存二维数组到txt文件中的方法
2018/11/15 Python
pyqt5 从本地选择图片 并显示在label上的实例
2019/06/13 Python
Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例
2019/08/01 Python
python 列表推导式使用详解
2019/08/29 Python
将数据集制作成VOC数据集格式的实例
2020/02/17 Python
解决Django中checkbox复选框的传值问题
2020/03/31 Python
详解anaconda安装步骤
2020/11/23 Python
食堂个人先进事迹
2014/01/22 职场文书
员工考核管理制度
2014/02/02 职场文书
工地安全检查制度
2014/02/04 职场文书
授权委托书怎么写
2014/04/03 职场文书
2014年医院工作总结
2014/11/20 职场文书
推销搭讪开场白
2015/05/28 职场文书
修改并编译golang源码的操作步骤
2021/07/25 Golang
Vertica集成Apache Hudi重磅使用指南
2022/03/31 Servers