Python Missing Image
So I want to create a window with a displayed image (from a specific file) and single button to close the window. So far it shows the window, resizes the window to fit the the imag
Solution 1:
The problem is that you need to keep your own reference to the PhotoImage
you create, or python will garbage collect it, because Tkinter doesn't keep a reference to it. This is either a bug or a feature, depending on your thinking. This code should solve your problem:
from Tkinter import *
import Image
import ImageTk
class MyApp:
def __init__(self, rData):
self.cont1 = Frame(rData)
self.cont1.pack(side="top", padx=5, pady=5)
self.button1 = Button(rData)
self.button1["text"]= "Exit"
self.button1["background"] = "red"
self.button1.pack(side="bottom",padx=5, pady=5, fill=X)
self.button1["command"]= rData.destroy
self.picture1 = Label(self.cont1)
self.photoImage = ImageTk.PhotoImage(Image.open("fire.ppm")) # This line prevents your photo image from getting garbage collected.
self.picture1["image"] = self.photoImage
self.picture1.pack(fill="both")
root = Tk()
myapp = MyApp(root)
root.mainloop()
Post a Comment for "Python Missing Image"