在Python中创建一个简单图像

在Python中创建一个简单图像,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我不知道如何在Python中显示来自不同目的地的图像。我需要知道如何使图像显示在我的程序上。我尝试使用以下代码,但它不会在Python程序中显示gif文件。我的文件名为“giftest.gif”,它与程序位于同一文件夹中,但它仍然不会显示,而是会弹出一个错误 from tkinter import * root = Tk() canvas = Canvas(root, width=500, height=500) canvas.pack() imagetest = Image("gifte

我不知道如何在Python中显示来自不同目的地的图像。我需要知道如何使图像显示在我的程序上。我尝试使用以下代码,但它不会在Python程序中显示gif文件。我的文件名为“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()

您需要使用
PhotoImage
而不是
Image

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()

您需要使用
PhotoImage
而不是
Image

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()

等等,我想我的程序搞错了。也许会有帮助。等等,我想我的程序出错了。也许有帮助。