-2

may you help me ?

I'm trying to make a software in python. I'm using tkinter and pil for training purposes.
I want to be able to create an empty image with PIL, draw on it and then make it appear with tkinter.

for now I have this

def redraw(self):
    self.canvas.delete("all")

    image = Image.new ('RGB', (100, 100))
    draw = ImageDraw.Draw(image)
    draw.rectangle ((0,0,100,100), fill = (20,20,20) )
    im = ImageTk.PhotoImage (image)
    self.canvas.create_rectangle (0,0, 100, 100) #appears
    self.canvas.create_image (0,0, image = im) #does not

Almost like the image created would be transparent.

Even if I add image.save("image.png", "PNG") which create an image with my desired gray rectangle.

Has anyone seen my flaw ?

Thank you dearly for your help.

NRagot
  • 113
  • 2
  • 12

1 Answers1

1

This is happening because the reference to 'im' is being lost - if you change 'im' to 'self.im', I think you will find that it works.

You may choose your own way of keeping the reference, of course. For a full explanation, see http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

radarhere
  • 929
  • 8
  • 23
  • thank you, never have I seen this issue before (I used to do a lot of java). Thank you a lot. – NRagot Jul 21 '18 at 12:06