0

I'm working on watermark desktop app using Tkinter. I've encountered a problem when, whenever I choose an image from a listbox of updated images and want to display them in my canvas box it shows only background.

So I've cut out part of code which I was hoping will update my images and tried to work out how it exactly works. I've figured out that when I am opening an image outside function which I expected to update image it works. When I open same image in this function canvas displays empty background in place where image should be. For sake of testing I've replaced listbox with button.

from tkinter import *

window = Tk()
window.geometry("1000x800")
window.config(padx=15, pady=15)

frm = Frame(window)
frm.grid()

canvas = Canvas(frm, width=350, height=350, bg="black")
myimg = PhotoImage(file = f"images/tibot.png")

image_container = canvas.create_image(175,175, image=myimg)
canvas.grid(column=2, row=0, padx=50)

# It updates canvas when this line is here.
newimg = PhotoImage(file = f"images/tictactoe.png")

def change_image():
    # It doesn't updates canvas when this line is here. 
    # newimg = PhotoImage(file = f"images/tictactoe.png")

    canvas.itemconfig(image_container, image=newimg)

Button(frm, text="Quit", command=change_image).grid(column=1, row=10)

window.mainloop()

I assume it have either something to do with scopes or that tkinter is running in a loop. If it changes much I can also post my listbox configuration but as fair as I think code above replicates my issue well enough.

Kossano
  • 1
  • 1

0 Answers0