5

I have the following python code:

from tkinter import *
from PIL import ImageTk, Image
import sys, os
height = 5
width = 8

# window = Tk()


class NSUI(Frame):
    def reload(self):
        os.execl(sys.executable, sys.executable, *sys.argv)
    def __init__(self, master=None):
        """
        Initialise process application
        """
        Frame.__init__(self, master)
        self.grid()
        self.master.title('PROGProject')
        # Configure columns
        for r in range(7):
            self.master.rowconfigure(r, weight=1)
        # Create columns
        for c in range(7):
            self.master.columnconfigure(c, weight=1)
            Button(master, text = "Actuele reistijden", bg = '#005ca0', fg = 'white', font = "Verdana 10", width = width, height = height, command = self.reload).grid(row = 5,column = 2,sticky = E+W)
            Button(master, text = 'Storingen', bg = '#005ca0', fg = 'white', font = "Verdana 10", width = width, height = height, command = self.reload).grid(row = 5,column = 4,sticky = E+W)

        Label(master, text = 'Welkom bij NS',font = "Verdana 50", bg = "#EFD10E", fg = "#005ca0").grid(row=0,column=1, columnspan=5)

        path = "test.jpg"
        img = ImageTk.PhotoImage(Image.open(path))
        panel = Label(root, image=img).grid(column=2,row=2)






root = Tk()

root.configure(background="#EFD10E")
root.tk.call('tk', 'scaling', 1.5)

app = NSUI(master=root)
app.mainloop()

It should load an image, the image is in the right directory, I made sure of that. but for some reason it just loads a white square. Does anyone know how this is possible?

The relevant bit of code that i have added :

path = "test.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(root, image=img).grid(column=2,row=2)

Thanks in advance.

Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • This may be relevant: [Why do my Tkinter images not appear?](http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm) – Kevin Nov 06 '17 at 14:06

1 Answers1

17

You have to anchor the image to the object.

path = "test.jpg"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(root, image=img)
panel.photo = img
panel.grid(column=2,row=2)
Avión
  • 7,963
  • 11
  • 64
  • 105