0

So hello, here I am once again. This time I am having trouble with Label having white background, even though my picture is transparent. How can I make Label background transparent, because background for the label will have gradients.

Here is simple code(Note that green background is going to have some type of gradient in the future):

from tkinter import *
from PIL import ImageTk, Image

root=Tk()
root.geometry("800x800")

canvas=Canvas(root, width=800,height=800)
canvas.create_rectangle(0,0,800,800, fill="green")
canvas.pack()

ClosePhoto = ImageTk.PhotoImage(Image.open('Close_PreHover.png'))
Close=Label(root,image=ClosePhoto)
Close.place(x=350,y=350)

root.mainloop()

Picture used in code

I know about the canvas.create_image() method, but that won't work for me since some of the commands are binded to the label, where my image is, and few of them change the current image with one slightly different. I have tried the attributes() method with -transparent, but I don't really want a whole in the window.

Any help will be of great value, so thank you in advance!

root.attributes("-transparent",color) method not working

  • Are you 100% sure your image is transparent? I've done a similar thing before (using PNG images with transparency on buttons) without issues. I can't see anything *immediately* wrong with your current implementation... – JRiggles Feb 09 '23 at 18:04
  • Yeah, I am 100% sure it is transparent, I drew it in Photoshop on the transparent background, and then saved it as .png. If you have a way to make a button transparent, so only the image is visible(if I understood you correctly), it could work, may you share that with me? – Luka Petrović Feb 09 '23 at 19:21
  • You can specify a transparent color with `root.attributes('-transparentcolor', '')`, so you could set it to something like `'magenta'` (or any color you're not using, really). Then anywhere that color is used (say, as the background of a `Label` or `Button`) will appear transparent. That said, if your root window isn't transparent, you'll still see it's background color through the other widgets/images. If you just want to remove the button *border*, you can set `relief='flat'` on any `tkinter.Button` widget (note that this trick doesn't work on `ttk.Button`s) – JRiggles Feb 09 '23 at 20:01
  • I am really not sure what I am doing wrong, but I did exactly as you have written. Now I have holes behind buttons, and I don't really want that... I will add it at the bottom of the question – Luka Petrović Feb 09 '23 at 21:16
  • 1
    You can bind events on the image that put in the canvas using `canvas.create_image()`. – acw1668 Feb 09 '23 at 23:30
  • @Luka Petrović. Can you post the original image? – toyota Supra Feb 10 '23 at 02:12
  • Can I modifiy canvas created image later or do I need to delete it, and create it again, acw? Png is posted above Toyota supra – Luka Petrović Feb 10 '23 at 06:25
  • Acw, I have used your ways once again, thank you very much!! – Luka Petrović Feb 10 '23 at 13:34

0 Answers0