python tkinter 图片插入问题

通过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)导入ImageImageTk模块来实现,代码如下:

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的图片插入可暂时告一段落。
如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

  • 21
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的大熊猫

你的鼓励将是我写作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值