2017-02-10 85 views
0

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

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

但我只是产生每个I运行程序时的错误......出于某种原因,光象永远不会为我工作。我如何得到这个工作?

+1

这是一个非常贫血的问题,提供更多的细节,如什么错误? (完整的堆栈跟踪会很好) – Nullman

+0

如果没有[mcve](http://stackoverflow.com/help/mcve),我无法确定,但这可能有助于http://stackoverflow.com/questions/16424091/putting -gif-in-a-canvas-with-tkinter –

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