Skip to main content
 首页 » 编程设计

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

2024年10月24日13me-sa

我可以在不使用 PIL 的情况下将照片放入 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() 

请您参考如下方法:

你可以只使用Tkinter.PhotoImage为了这。最小示例:

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

不过要小心,那些 PhotoImages 往往是 garbage-collected即使在 Label 中使用,所以您应该在全局范围内定义它,或者将它添加到某个全局列表或字典中。