【问题标题】:How to insert a photo into a Tkinter window no PIL of pillow如何将照片插入 Tkinter 窗口没有枕头的 PIL
【发布时间】:2015-08-12 08:00:50
【问题描述】:

我可以在不使用 PIL 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()

【问题讨论】:

  • P.S 我不是故意留下关于项目的文字...

标签: python tkinter


【解决方案1】:

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

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

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

【讨论】:

  • 这不适用于 PNG、JPG、BMP 图像。有没有使用这些文件类型的解决方案
  • 我一直遇到图像完全空白的问题....这使我进入了这个精彩的页面:effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm 他们指出需要保留对该对象的引用。一旦我添加了代码行来执行此操作,我就可以了... tktext_label.image = photo 我能够让它与 PNG 图像一起使用,这非常好。如果这意味着我不必导入任何其他包,我对 PNG 感到满意。
  • @MikeyB 是的,我在帖子的最后一行试图解决的问题。
  • 对....只是想澄清如果您不执行诸如保留引用之类的操作以及提供可以执行此操作的代码会发生什么。使用提供的代码,我看到了一个空白方块,我的图像应该是。
  • 感谢您对“垃圾收集”图片的评论!!!我浪费了几个小时的空白屏幕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
相关资源
最近更新 更多