2

Im trying to make a game engine with tkinter but I've encountered a problem. I have an image when opened, clearly shows transparency and, when this code is run:

print(image.mode)

it prints RGBA as the mode. I have googled the necessary elements for transparent images on tkinter and I have all of it. Mode=RGBA format=PNG and yet, it's still not completely transparent:

enter image description here

and here's the image when opened on preview. It clearly shows transparency:

enter image description here

So why does it show transparency in preview and other apps(like google docs, slides, etc.) but doesn't show transparency in tkinter?

heres the full code:

from tkinter import *
from PIL import ImageTk, Image

class Create(object):
  def __init__(self, root, img="default.png", width=None, height=None, x=0, y=0):
    self._debug_img = Image.open(img)
    self.img = ImageTk.PhotoImage(Image.open(img))
    
    self.img_wdth, self.img_hgt = self._debug_img.size

    if width == None and height == None:
        self.width = self.img_wdth
        self.height = self.img_hgt

    elif type(width) != int and type(height) != int:
        self.width = self.img_wdth
        self.height = self.img_hgt
    
    elif width != self.img_wdth and height != self.img_hgt:
        self.copy = self._debug_img.resize((self.width, self.height))
        self.img = ImageTk.PhotoImage(Image.open(self.copy))

    self.hitbox = (width, height)
    self.x = x
    self.y = y

    self.root = root

    self.sprite = Label(self.root, image=self.img)
    self.sprite.place(x=self.x, y=self.y, anchor=CENTER)

  def update_pos(self):
    self.sprite.place(x=self.x, y=self.y)

  def update_sprite(self):
    self.copy = ImageTk.PhotoImage(Image.open(Image.open(self.img).resize(self.width, self.height)))
    self.sprite = Label(self.root, image=self.copy)

  def update_hitbox(self):
    self.hitbox = (self.width, self.height)
rioV8
  • 24,506
  • 3
  • 32
  • 49
coder lol
  • 49
  • 6
  • You used a label to show the image but tkinter widgets do not support transparent background. So even though the image is transparent, but the label is not. – acw1668 May 20 '22 at 23:36

1 Answers1

1

Steps to solve: Make sure your Image is indeed a PNG and RGBA , Then you must put your image inside a tkinter canvas canvas.create_image(150, 150, image=photoimage) Here is a modified code and its result from How do I make Tkinter support PNG transparency? for Python 3

from tkinter import Tk, Frame, Canvas
from PIL import ImageTk, Image



from PIL import Image
im = Image.open("default.png")

if im.mode in ["RGBA","P"]:


    #Displaying:
    t = Tk()
    t.title("Transparency")

    frame = Frame(t)
    frame.pack()

    canvas = Canvas(frame, bg="black", width=500, height=500, bd=0, highlightthickness=0)
    canvas.pack()

    photoimage = ImageTk.PhotoImage(file="default.png")
    canvas.create_image(150, 150, image=photoimage)#,alpha=0.5)#,bg="black")

    t.mainloop()
else:
    print("Can't display transparency since image mode is ",im.mode)
    input()

The Example

Wish you best!

DanielGzgzz
  • 118
  • 1
  • 6
  • it shows a black screen when I implement canvas – coder lol May 21 '22 at 01:01
  • Hi, its because it is set to it at `canvas = Canvas(frame, bg="black", width=500, height=500, bd=0, highlightthickness=0)` , the `bg="black"` section, change it to whatever you require, Have it answered your question? – DanielGzgzz May 21 '22 at 01:17
  • i got that fixed, but now it shows a large white square. It's still not transparent and an image is not even in it. it's also not its original small size. errors: giant white square with no image still not completely transparent not its original size – coder lol May 21 '22 at 01:41
  • Have you made sure your image is PNG and RGBA? – DanielGzgzz May 21 '22 at 01:50
  • extension clearly shows PNG, image.mode gives RGBA. I am sure its PNG and RGBA – coder lol May 21 '22 at 17:22
  • well about the size you can adjust the image size using PIL `width = 50 height = 50 img = Image.open("dir.png") img = img.resize((width,height), Image.ANTIALIAS) photoImg = ImageTk.PhotoImage(img)` https://stackoverflow.com/questions/6582387/image-resize-under-photoimage , Idon't know what to tell you about the image, can you please send me a link for the image so i try for myself? , at its original format and data – DanielGzgzz May 21 '22 at 17:31
  • https://imageerror.nathanli1818.repl.co/ I don't know how to post an image onto comments so that's the best I can do. You can inspect and go to sources to find the original image. – coder lol May 21 '22 at 18:23
  • https://prnt.sc/_fKrmccsIHUz , there's absolutely no problem here with python 3.7 with your image , on Windows 10, maybe your OS which is Mac causing this, I found an answer that solves the issue for Mac-OS : https://stackoverflow.com/a/44296157/10634282 try reading there :) I hope this would solve this for you – DanielGzgzz May 21 '22 at 20:18
  • thanks for the help. If I hadn't discover this line of code. label.config(bg='systemTransparent') I wouldn't have been able to fix it. – coder lol May 21 '22 at 21:56