Image overlay in Tkinter

Asked

Viewed 821 times

3

I have a problem trying to overwrite an 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()
  • Even if I place the image in a different position than the other one the image n appears in itself but occupies space

1 answer

2


Good morning buddy!

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 property file label: head['file'] = "rosto1.png"

Follows 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 there and send feedback if it solves your problem.

  • Face worked yes, mto thanks msm, I did not know that could only alter the path in python, vlw msm

  • 1

    Good! You can change any attribute..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.