当前位置: 移动技术网 > IT编程>脚本编程>Python > 详解python tkinter 图片插入问题

详解python tkinter 图片插入问题

2020年09月04日  | 移动技术网IT编程  | 我要评论
通过tkinter.photoimage插入gif, pgm/ppm格式的图片。import tkinterclass gui: def __init__(self): self.gui=

通过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 图片插入内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网