python – Image overlay on Tkinter

Question:

I have a problem trying to overwrite an already existing image in Python 3.

from tkinter import *
jan = Tk()
jan.geometry("500x500")
jan.configure(background="#f0f0f0")

head = PhotoImage(file = "rosto0.png")
label = Label(jan, image = head)
label.place(x=100 , y=10)

def crt():
   head2 = PhotoImage(file = "rosto1.png")
   lbl = Label(jan, image = head2)
   lbl.place(x=100 , y=10)

crt = Button(jan, text="TROCAR ROSTO", font=("Centurty Gothic",10), 
command=crt)
crt.place(x=5, y=10)

jan.mainloop()

Answer:

Good morning my friend!

I solved your problem here in a very simple way:

I used only one label and when the button is clicked, I simply changed the label's file property: head['file'] = "rosto1.png"

Here's the code:

from tkinter import *

jan = Tk()
jan.geometry("500x500")
jan.configure(background="#f0f0f0")

head = PhotoImage(file = "rosto0.png")
label = Label(jan, image = head)
label.place(x=100 , y=10)


def crt():
   head['file'] = "rosto1.png"


crt = Button(jan, text="TROCAR ROSTO", font=("Centurty Gothic",10), command=crt)
crt.place(x=5, y=10)

jan.mainloop()

If you can, test it there and send a feedback if it solves your problem.

Scroll to Top