如何将照片插入Tkinter窗口,没有PIL的枕头

时间:2015-08-12 08:00:51

标签: python tkinter

我可以在不使用Pill of Pillow的情况下将照片放入tkinter窗口吗?我希望将照片插入label1

这是原始剧本:

from Tkinter import *
import tkMessageBox

root=Tk()

root.title("Moldy Cheese Quiz")
root.geometry("500x500")
root.config(background="red")

f1=Frame()
f1.pack()
f1.config(background="red")

text=Text(f1)

title= """This is a quiz to see if you've been listening!"""

text.delete(1.0, END)
text.insert(END, title)
text.config(width=47, height=5, state="disabled")
text.pack()

b=Button(f1)

def draw():
    photo = trollface.gif

def destroy():
    f1.destroy(), f2.pack(), root.config(background="blue")

def good1():
    f2.destroy(), f3.pack(), root.config(background="purple")

def good2():
    f3.destroy(), f4.pack(), root.config(background="green")

def good3():
    f4.destroy(), f5.pack(), root.config(background="pink")

def wrong():
    tkMessageBox.showinfo("Wrong Answer", "You were not listening so you got it wrong!")

b.config(text="Start quiz", command=destroy)
b.pack()

f2=Frame()
f2.config(background="blue")
f3=Frame()
f3.config(background="purple")
f4=Frame()
f4.config(background="green")
f5=Frame()
f5.config(background="pink")
f6=Frame()
f6.config(background="orange")

text2=Text(f2)
text2.pack(side=TOP)
text2.insert(END, "How do you calculate a food mile?")
text2.config(width=37, height=10, state="disabled")

b2=Button(f2)
b2.pack(side=TOP)
b2.config(text="Times the distance all ingredients travelled by the carbon  intensity.", command=good1)

b3=Button(f2)
b3.pack(side=TOP)
b3.config(text="Divide the distance all the ingredients travelled by the  carbon intensity.", command=wrong)


b4=Button(f2)
b4.pack()
b4.config(text="Subtract the number of the ingredients from the distance.", command=wrong)

text3=Text(f3)
text3.pack()
text3.insert(END,"Where does the most of our food come from?")
text3.config(width=42, height=10, state="disabled")


b5=Button(f3)
b5.pack()
b5.config(text="Ireland", command=wrong)

b6=Button(f3)
b6.pack()
b6.config(text="China", command=good2)

b7=Button(f3)
b7.pack()
b7.config(text="Australia", command=wrong)

b8=Button(f3)
b8.pack()
b8.config(text="India", command=wrong)

text4=Text(f4)
text4.pack()
text4.insert(END, "What is the main reason we buy imported food?")
text4.config(width=45, height=5, state="disabled")

b9=Button(f4)
b9.pack()
b9.config(text="It is better quality.", command=wrong)

b10=Button(f4)
b10.pack()
b10.config(text="It looks nicer.", command=wrong) 

b11=Button(f4)
b11.pack()
b11.config(text="It is cheaper.", command=good3)

b12=Button(f4)
b12.pack()
b12.config(text="We don't make food.", command=wrong)

text5=Text(f5)
text5.pack()
text5.insert(END, "Ok. Good Job. MOST of you were listening...")
text5.config(width=43, height=1, state="disabled")

label1=Label(f5)
label.pack()
root.mainloop()

1 个答案:

答案 0 :(得分:2)

您可以使用Tkinter.PhotoImage。最小的例子:

root = Tkinter.Tk()
image = Tkinter.PhotoImage(file="img.gif")
Tkinter.Label(root, image=image).pack()
root.mainloop()

请注意,即使在PhotoImages中使用Label~/rpmbuild/SOURCES也往往是garbage-collected,因此您应该在全局范围内定义它,或者将其添加到全局范围内列表或字典。