I've seen almost every post about this issue and nothing solved it. At this point I'm not getting an error, it's just not working at all.
So, I'm just making a silly program with TKinter to simulate the DVD bouncing logo, which works, but doesn't change the colors (in this case I have 4 different images that I try to change, but are not changing)
import random
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("DVD Logo")
root.resizable(False, False)
root.attributes("-fullscreen", True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
dvd_vermelho = Image.open("dvd_vermelho.png").resize((256, 256))
dvd_verde = Image.open("dvd_verde.png").resize((256, 256))
dvd_azul = Image.open("dvd_azul.png").resize((256, 256))
dvd_amarelo = Image.open("dvd_amarelo.png").resize((256, 256))
x,y = dvd_vermelho.size
canvas_x = width
canvas_y = height
canvas = Canvas(root, width=canvas_x, height=canvas_y, bg="blue")
canvas.pack()
root.update()
canvas_widht = canvas.winfo_width()
canvas_height = canvas.winfo_height()
dvd_vermelho_tk = ImageTk.PhotoImage(dvd_vermelho, master=canvas)
dvd_verde_tk = ImageTk.PhotoImage(dvd_verde, master=canvas)
dvd_amarelo_tk = ImageTk.PhotoImage(dvd_amarelo, master=canvas)
dvd_azul_tk = ImageTk.PhotoImage(dvd_azul, master=canvas)
dvd_logos = [dvd_amarelo_tk, dvd_verde_tk, dvd_vermelho_tk, dvd_azul_tk]
containerDVD = canvas.create_image(0, 0, anchor=NW, image=dvd_logos[0])
valuex = 2
valuey = 3
random_x = random.randint(0, canvas_widht - x)
random_y = random.randint(0, canvas_height - y)
canvas.move(containerDVD, random_x, random_y)
initial_trajectory_x = random.choice([-valuex, valuex])
initial_trajectory_y = random.choice([-valuey, valuey])
current_posx = random_x
current_posy = random_y
def changeImage():
global dvd_azul_tk, dvd_verde_tk, dvd_vermelho_tk, dvd_amarelo_tk, containerDVD
last_exit = dvd_logos.pop(0)
dvd_logos.append(last_exit)
canvas.delete(containerDVD)
containerDVD = canvas.create_image(current_posx, current_posy, anchor=NW, image=dvd_logos[0])
def moveDVD():
global current_posx, current_posy, initial_trajectory_x, initial_trajectory_y, dvd
canvas.move(containerDVD, initial_trajectory_x, initial_trajectory_y)
current_posx += initial_trajectory_x
current_posy += initial_trajectory_y
if current_posx <= 0 or current_posx+x >= canvas_widht:
initial_trajectory_x *= -1
changeImage()
if current_posy <= 0 or current_posy+y >= canvas_height:
initial_trajectory_y *= -1
changeImage()
root.after(12, moveDVD)
root.after(12, moveDVD)
root.mainloop()
The real issue is in this line of code:
def changeImage():
global dvd_azul_tk, dvd_verde_tk, dvd_vermelho_tk, dvd_amarelo_tk, containerDVD
last_exit = dvd_logos.pop(0)
dvd_logos.append(last_exit)
canvas.delete(containerDVD)
containerDVD = canvas.create_image(current_posx, current_posy, anchor=NW, image=dvd_logos[0])
Here I try to delete the previous image and create another one. I've already tried canvas.itemconfig(containerDVD, image=dvd_logos[0]) too but it doesn't work either. Also tried updating, but didn't work.
Is there any steps I'm missing? Is this about the image reference? Or is it about my after loop?
Guides I've seen to help solving the problem: Tkinter canvas fails to update image - https://www.tutorialspoint.com/how-to-update-an-image-in-a-tkinter-canvas 'image "pyimage2" doesn't exist'? (this error happend before I transformed everything in PhotoImage) Why does Tkinter image not show up if created in a function? - Python Tkinter ~ canvas.delete()