0
import tkinter as tk
from tkinter import *
from PIL import Image,ImageTk
from PIL import ImageTk
from tkinter import filedialog
import sys
# # # 
root=tk.Tk()
root.geometry('1000x690')
root.title("Baccalauréat ISN 2017")

def Open_Image():
    Im =Image.open(filedialog.askopenfilename())

# i want to be able to resize the image without deforming it.
    Nim = Im.resize((int((Im.width*514)/Im.height), 514))   #maxsize =     (821, 514) ---> size of the canvas 821-length; 514 -height
    nshow = ImageTk.PhotoImage(Nim)

    Can = tk.Canvas(root, background = 'blue')
    Can.grid(row = 1, column = 0, rowspan = 6, columnspan = 5, sticky = W + E + N + S)
    Cim = Can.create_image(0, 0,  anchor = NW, image = nshow)

B13= Button(root, text='Open Image', height=5, width= 25, command = Open_Image)
B13.grid(row=1, column=5, sticky= W + E)


mainloop()

Why is the code not working ? It shows this (a blank canvas), instead of the selected file it should show in its place. enter image description here

Jos Maesen
  • 21
  • 1

1 Answers1

0

It is likely getting caught in garbage collection. To prevent this, bind it to a reference, i.e. put root. in front of the name you give your image in this case or something similar.

SneakyTurtle
  • 1,831
  • 1
  • 13
  • 23