1

Hi I want to click on a picture in the canvas in order to open a new canvas in which there is another picture and for some reason the new canvas opens and the picture does not appear anyone might know how to fix this?

from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.title("root")
root.geometry('800x550')

canvas = Canvas(root,
                bg='white',
                bd=5,
                relief='groove',
                height=500,
                width=700,
                )
canvas.place(x=80, y=0)

def press_to_open(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))
    new_img = Image.open("images/back_p.png")
    w, h = new_img.size

    if x >= 3 and x <= 3 + w and y >= 3 and y <= 3 + h:
        canvas2 = Canvas(canvas,
                         bg='white',
                         bd=2,
                         relief='groove',
                         height=120,
                         width=250,
                         )
        canvas2.place(x=100, y=90)
        resize_text = Label(canvas2, text="Resize the image:", bg="white", font=('Bodoni MT', 
        12))
        resize_text.place(x=65, y=10)

        img = PhotoImage("images/x.png")
        x_button = Button(canvas2, command=canvas2.destroy, bd=0, image=img)
        x_button.pack()
        canvas2.create_window(10, 10, window=x_button, height=65, width=65)

def destroy_window(event):
    global new_label
    new_label.destroy()

color_img = ImageTk.PhotoImage(Image.open("images/back_p.png"))
label = Label(canvas, image=color_img)
label.place(x=10, y=10)
root.bind('<Button-1>', press_to_open)
root.bind('<Button-3>', destroy_window)
root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • 2
    `img = PhotoImage("images/x.png")` should be `img = PhotoImage(file="images/x.png")`. After fixing it, it will have same issue as this [`question`](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). – acw1668 Mar 09 '22 at 09:27

0 Answers0