Reputation: 1185
Can I put a photo into a tkinter window without using PIL of Pillow? I would like the photo to be inserted into label1
.
Here is the original script:
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()
Upvotes: 3
Views: 12940
Reputation: 82889
You can just use Tkinter.PhotoImage
for this. Minimal example:
root = Tkinter.Tk()
image = Tkinter.PhotoImage(file="img.gif")
Tkinter.Label(root, image=image).pack()
root.mainloop()
Be wary, though, that those PhotoImages
tend to be garbage-collected even when used in a Label
, so you should either define it on global scope, or add it to some global list or dictionary.
Upvotes: 7