0

In my program, I want to show an image in a canvas which I created before through a button command.

Here is my code:

from Tkinter import Tk,Button,Canvas,PhotoImage
from PIL import Image, ImageTk



def dessiner ():

    

    # open the image 
    image = Image.open("img1.png")
    image = image.resize( (400,300), Image.ANTIALIAS) # resize the image to be show in the canvas
    img = ImageTk.PhotoImage(image)
    canvas.create_image(200, 175, image = img)
    
    

    return 0

mw = Tk()
canvas = Canvas(mw, \
width =400, height =650, \
bg="light yellow")
canvas.pack(side ="right")
button_plot = Button(mw, text = "dessiner", command = dessiner, width = 50, fg = "red")
button_plot.pack()
mw.mainloop()
Community
  • 1
  • 1
mounir ben salem
  • 366
  • 1
  • 2
  • 11
  • My first guess would be that once `dessiner()` has been called, you lose the reference to `img`. In order for the image to be displayed correctly, you need to keep a reference to it. See [this question](http://stackoverflow.com/questions/16846469/tkinter-canvas-image-is-not-displayed-in-class). – Iodestar Apr 04 '16 at 20:04
  • i don't think so, finally i find the solution by using the method ( .show() ) – mounir ben salem Apr 04 '16 at 21:26

1 Answers1

0

I find the solution using the method .show()

 plt.show()
mounir ben salem
  • 366
  • 1
  • 2
  • 11
  • This makes no sense. The code in the question doesn't have a `plt`` object. – Bryan Oakley Apr 07 '16 at 11:36
  • @BryanOakley it works for me ! and i got a result – mounir ben salem Apr 07 '16 at 12:43
  • I'm sure there is lots of code that works for you. If it has nothing to do with the question being asked, it's a worthless answer. Stackoverflow isn't just for your benefit. Others may find your question when trying to solve their problems, and an answer that is a line of code that uses an object that is not part of the question is useless. – Bryan Oakley Apr 07 '16 at 12:45
  • in my code, there is nothing related to the package "matplotlib" . i don't understand how it works in my code, i put it by hazard and it works finally. – mounir ben salem Apr 08 '16 at 11:17
  • it works for me, so it might works for an other !!!! – mounir ben salem Apr 08 '16 at 11:24
  • You are completely missing the point. It can't possibly work for another because they won't have a `plt` object. You simply _must_ have a `plt` object for this to work. Not only a `plt` object, but one that has a `show` method. You can't fix code by just trying random statements. Your advice here is absolutely no different than if you had said "the solution is to call `xyz.foobar()`, – Bryan Oakley Apr 08 '16 at 11:34
  • before using plt.show() i added : import matplotlib.pyplot as plt !! – mounir ben salem Apr 08 '16 at 14:29