How do I correctly use the label to display an image in tkinter since every time the window is opened, the image is displayed, but it appears completely white, not showing its relevant content?
I have this doubt because the format of the image used in the following code is PNG, and the image is transparent with solid content, yet after running the code, it appears only as a totally white label. For more useful information, I refer to this code as an initial stage of the hangman game, the image in question is stored in the variable image2, which is where the error is occurs. The variable image1 is not called in this case.
from tkinter import *
from tkinter import ttk
root = Tk()
root.configure(background="#4F4F4F")
root.option_add('*tearOff', False)
def images():
image1 = PhotoImage(file='Boneco1.png')
image1 = image1.subsample(1, 3)
image2 = PhotoImage(file="Bonecolindo.png")
image2 = image2.subsample(1, 1)
return [image1, image2]
class Game:
def __init__(self):
root.title("TWG")
root.geometry("900x700")
label = ttk.Label(root, text="The Words Game", font=("Times New Roman", 18), background="#4F4F4F", foreground="white")
label.place(relx=0.5, y=30, anchor="center")
image = Label(root, image=images()[0], borderwidth=2, relief="solid", bg="gray")
image.place(x=0, rely=0.5, anchor="center")
imagel = Label(root, image=images()[1])
imagel.place(relx=0.5, rely=0.5, anchor="center")
Game()
root.mainloop()
1- I have reformulated the image into various compatible formats, and now I'm using the PNG format.
2- I went an image editor, GIMP, and created an alpha channel for the image background. I tried reformulating the code multiple times, taking the image variable out of the images()
function and putting it inside the init()
function, but the same thing happened.
3- I have already tried modifying the image, and I also tested several other images to see if the issue was with the file, but other issues occurred.
I am using a specific library, so I hope to not have to use another library unless it is necessary.