title: Displaying an image in python via Button and Canvas
body: <p>I am a begginer in python, tkinter. I have written a code that should normally display an image in a canvas.</p>
  
  <p>What happens is that the main frame (gui) is displayed with the menu bar, then when I click on load image, the gui window shrinks (to 100x100 I guess) but nothing is displayed within.</p>
  
  <p>Could you please explain to me why this is happening so I can understand where the error occurs, and how to correct it?</p>
  
  <pre><code># -*- coding:utf-8 -*-
  # Imports
  from tkinter import Tk, Menu, Canvas
  from PIL import Image, ImageTk
  
  # Function definitions
  def deleteImage(canvas):
      canvas.delete("all")
      return
  
  def loadImage(canvas, img):
      filename = ImageTk.PhotoImage(img)
      canvas.image = filename
      canvas.create_image(0,0,anchor='nw',image=filename)
      return
  
  def quitProgram():
      gui.destroy()
  # Main window
  gui = Tk()
  
  # Inside the main gui window
  #Creating an object containing an image
  # A canvas with borders that adapt to the image within it
  img = Image.open("fleur.jpg")
  canvas = Canvas(gui,height=img.size[0],width=img.size[0])
  canvas.pack()
  
  # Menu bar
  menubar = Menu(gui)
  # Adding a cascade to the menu bar:
  filemenu = Menu(menubar, tearoff=0)
  menubar.add_cascade(label="Files", menu=filemenu)
  # Adding a load image button to the cascade menu "File"
  filemenu.add_command(label="Load an image", command=loadImage)
  # Adding a delete image button to the cascade menu "File"
  filemenu.add_command(label="Delete image", command=deleteImage)
  filemenu.add_separator()
  filemenu.add_command(label="Quit", command=quitProgram)
  menubar.add_separator()
  menubar.add_cascade(label="?")
  
  # Display the menu bar
  gui.config(menu=menubar)
  gui.mainloop()
  </code></pre>
  
  <p>EDIT:
  The second problem is that I want to create a canvas and the image in the main gui window, and pass them as arguments to the menu buttons (See code above, where img and canvas are created separately from the function <code>loadImage</code>). Seeing as putting parenthesis in the <code>command=loadImage()</code> created a problem on its own.</p>
  
  <p>Another point that rises a question in my head : Regarding the first problem which was solved by keeping a reference to the <code>filename=ImageTk.PhotoImage(img)</code>. Wouldn't it normally be pointless to keep a reference inside the function since it's a local variable anyway?</p>