Search code examples
pythonimagetkinterbuttonpyautogui

Image keeps getting deleted


enter image description here

I'm a new to coding but started since I need to create some software. I have a problem where the images that are attached to a button delete themselves. What I mean is I have 2 images and 2 buttons, I want to press any button and I want images to stack next to each other instead of deleting one. I would appreciate any help!

def add_image():
    global my_image
    my_image = PhotoImage(file="Picture1.PNG")
    my_text.image_create(root, command=my_image)

def add_image2():
    global my_image
    my_image = PhotoImage(file="Picture2.PNG")
    my_text.image_create(END, image=my_image)

I'm guessing the "END" line makes them delete themselves but I haven't figured what to replace it with.


Solution

  • It is better to create the two images in global scope:

    my_image1 = PhotoImage(file="Picture1.PNG")
    my_image2 = PhotoImage(file="Picture2.PNG")
    
    def add_image():
        my_text.image_create(END, image=my_image1)
    
    def add_image2():
        my_text.image_create(END, image=my_image2)