Tkinter PhotoImage无法使用

时间:2017-02-10 00:22:57

标签: python tkinter photoimage

我一直在创建一个在画布上显示图像的文件。我创建了'PhotoImage'文件,用于存储我的图片。

 d = PhotoImage(file="pic.gif")
 canvas = Canvas(root, 500, 500)
 pic = canvas.create_image(20, 20, image=d)

但是我每次运行程序时都会产生错误......出于某种原因,PhotoImage永远不会对我有用。我如何让它工作?

1 个答案:

答案 0 :(得分:0)

这是一个适合我的例子。

#----------------------------------------------------------------------
import Tkinter
#----------------------------------------------------------------------
root = Tkinter.Tk()
frame = Tkinter.Frame(root)
frame = Tkinter.LabelFrame(root, text="LabelFrame text", padx=5, pady=5)
frame.pack(side=Tkinter.LEFT)
Label1 = Tkinter.Label(frame, text="Label1 text")
Label1.pack()
#----------------------------------------------------------------------
photo1 = Tkinter.PhotoImage(file = 'Chart_Example.gif')
#
width_1 = photo1.width()
height_1 = photo1.height()
#
x_center_1 = width_1 / 2.0
y_center_1 = height_1 / 2.0
#---------------------------------
iframe1 = Tkinter.Frame(frame, bd=2, relief=Tkinter.RAISED)
iframe1.pack(expand=1, fill=Tkinter.X, pady=5, padx=5, side=Tkinter.LEFT)
c1 = Tkinter.Canvas(iframe1, width=width_1, height=height_1)
c1.create_image(x_center_1, y_center_1, image=photo1, anchor = Tkinter.CENTER)
c1.pack(side=Tkinter.LEFT)
#----------------------------------------------------------------------
root.mainloop()
#----------------------------------------------------------------------