用Python创建一个简单的图像

Ron*_*ang 0 python tkinter python-3.x

我不确定如何在Python中显示来自不同目的地的图像.我需要知道如何让图像显示在我的程序上.我尝试使用以下代码,但它不会将gif文件显示在Python程序中.我的文件名为"giftest.gif",它与程序位于同一文件夹中,但仍然无法显示,而是弹出错误.

from tkinter import *

root = Tk()

canvas = Canvas(root, width=500, height=500)
canvas.pack()

imagetest = Image("giftest.gif")
canvas.create_image(250, 250, image=imagetest)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

Bla*_*der 5

您需要使用PhotoImageImage.

from tkinter import *

root = Tk()

canvas = Canvas(root, width=500, height=500)
canvas.pack()

imagetest = PhotoImage(file="file.gif")
canvas.create_image(250, 250, image=imagetest)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)