2017-02-10 56 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() 
#----------------------------------------------------------------------