2014-01-31 33 views
0

这是到目前为止我的代码:需要帮助的Tkinter设置.gif图像

from tkinter import * 

root = Tk() 
root.title("Skin") 

def frame(): 
    f = Frame(root, height=350, width=400) 
    f.pack_propagate(0) 
    f.pack() 

def credit(): 
    print("") 

def image1(): 
    skin1 = PhotoImage(file='C:/Users/Xiam/Desktop/1.gif') 

b = Button(root, text="Click here to see the diagram!", command=credit) 
b.pack(fill=X, expand=2, anchor=N) 

frame() 
image1() 

root.mainloop() 

我知道这可能不是非常有效的,但我只是掀起这件事像两分钟。无论如何,我一直试图让我的dektop上的“1.gif”图像显示在Tkinter窗口中,并且它不起作用。完全一样。我究竟做错了什么?

回答

2

您正在创建一个图像对象,但您没有将其显示在屏幕上。您需要将它与Label小部件相关联,或将其嵌入到Canvas或Text小部件中。您可以在创建图像对象时创建标签图像,也可以提前创建标签,并在创建图像对象时对其进行更改。

例如:

skin1=PhotoImage(...) 
the_label = Label(root, image=skin1) 

- 或 -

the_label = Label(root) 
... 
skin1=PhotoImage(...) 
the_label.configure(image=skin1) 
+0

...还记得要保持一个全球参考'PhotoImage',否则它就会被垃圾回收。 –