详解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中Continue语句的用法的举例详解
May 14 Python
全面了解Nginx, WSGI, Flask之间的关系
Jan 09 Python
Python基于win32ui模块创建弹出式菜单示例
May 09 Python
python通过微信发送邮件实现电脑关机
Jun 20 Python
python 顺时针打印矩阵的超简洁代码
Nov 14 Python
如何通过python的fabric包完成代码上传部署
Jul 29 Python
Python定时任务随机时间执行的实现方法
Aug 14 Python
Python高级编程之消息队列(Queue)与进程池(Pool)实例详解
Nov 01 Python
python 操作hive pyhs2方式
Dec 21 Python
使用 tf.nn.dynamic_rnn 展开时间维度方式
Jan 21 Python
如何使用python的ctypes调用医保中心的dll动态库下载医保中心的账单
May 24 Python
Python3爬虫RedisDump的安装步骤
Feb 20 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
2020年4月新番动漫目录 官方宣布4月播出的作品一览
2020/03/08 日漫
PHP curl批处理及多请求并发实现方法分析
2018/08/15 PHP
基于jquery的跟随屏幕滚动代码
2012/07/24 Javascript
下拉列表select 由左边框移动到右边示例
2013/12/04 Javascript
js生成随机数之random函数随机示例
2013/12/20 Javascript
使用jQuery时Form表单元素ID和name命名大忌
2014/03/06 Javascript
js淡入淡出的图片轮播效果代码分享
2015/08/24 Javascript
原生JS查找元素的方法(推荐)
2016/11/22 Javascript
jQuery上传插件webupload使用方法
2017/08/01 jQuery
vue实现手机号码抽奖上下滚动动画示例
2017/10/18 Javascript
jQuery ajax读取本地json文件的实例
2017/10/31 jQuery
基于JavaScript 性能优化技巧心得(分享)
2017/12/11 Javascript
Vuejs 2.0 子组件访问/调用父组件的方法(示例代码)
2018/02/08 Javascript
D3.js实现简洁实用的动态仪表盘的示例
2018/04/04 Javascript
vue 组件销毁并重置的实现
2020/01/13 Javascript
JS实现移动端可折叠导航菜单(现代都市风)
2020/07/07 Javascript
python获得文件创建时间和修改时间的方法
2015/06/30 Python
Python微信库:itchat的用法详解
2017/08/14 Python
python微信公众号之关注公众号自动回复
2018/10/25 Python
python的pytest框架之命令行参数详解(上)
2019/06/27 Python
python基于json文件实现的gearman任务自动重启代码实例
2019/08/13 Python
pygame实现成语填空游戏
2019/10/29 Python
python+tifffile之tiff文件读写方式
2020/01/13 Python
PyQt5中QSpinBox计数器的实现
2021/01/18 Python
松下电器美国官方商店:Panasonic美国
2016/10/14 全球购物
学年自我鉴定范文
2013/10/01 职场文书
客服专员岗位职责
2014/02/28 职场文书
大学军训感言1500字
2014/03/09 职场文书
安全生产目标责任书
2014/04/14 职场文书
党的群众路线教育实践活动个人整改措施
2014/10/27 职场文书
2014司机年终工作总结
2014/12/05 职场文书
初中优秀学生评语
2014/12/29 职场文书
2015年超市员工工作总结
2015/05/04 职场文书
刑事案件上诉状
2015/05/23 职场文书
Python激活Anaconda环境变量的详细步骤
2021/06/08 Python
Python+Matplotlib+LaTeX玩转数学公式
2022/02/24 Python