Image won't display on tkinter canvas

661 views Asked by At

I'm really hoping for some help on this as it has me absolutely stumped. I have the code working on its own as per below:

from tkinter import *
from PIL import ImageTk

dam_level = [75]

c = Canvas(width = 200, height = 235, relief = "sunken", borderwidth = 2)
c.grid(row = 11, rowspan = 8, column = 4, columnspan = 2)

c_width = 200 
c_height = 250

y_stretch = 1.9

y_gap = 35

x_stretch = 15
x_width = 90

x_gap = 30

for x, y in enumerate(dam_level):

    x0 = x * x_stretch + x * x_width + x_gap
    y0 = c_height - (y * y_stretch + y_gap)
    x1 = x * x_stretch + x * x_width + x_width + x_gap
    y1 = c_height - y_gap

    c.create_rectangle(x0, y0, x1, y1, fill = "#008ae8")

    y = (str(y))
    c.create_text(x0 + 10, y0, anchor = SW, text = (y, "%"))
    c.create_text(x0 + 60, y1 + 5, anchor = N, text = "Catchment")
    photo = ImageTk.PhotoImage(file =    
    "/Users/Name/Desktop/python3.4/water.png")
    c.create_image(10, 10, image = photo, anchor = NW)

mainloop()

However when I put it in my main application in its own function (with the rest of my code), the image won't display. The graph and canvas displays, just not the water.png image. There's no error log or anything. The only change I make when I put this in my app is adding 'self' to this line. (And I remove the 'mainloop()' of course).

c = Canvas(self, width = 200, height = 235, relief = "sunken", borderwidth = 2)

Any suggestions would be greatly appreciated.

1

There are 1 answers

0
SGB On

Thanks Bryan, for pointing me in the right direction.

Fixed with:

c.image = photo