1

The reason for this question is for informational purposes, also to provide the beginning reader with the correct path to follow if you intend to display a png image without a background, in a tkinter window. Try wm_attributes ("-transparentcolor", " "), but it is not an absolute and correct solution. Thank you very much for your patience and recommendation.

from tkinter import Tk,Frame,Label,PhotoImage

root = Tk()
image = PhotoImage(file="image.png")

frame = Frame(root)
label = Label(frame, image=image)
label.pack(side="bottom", fill="both")
frame.pack(side="bottom", fill="both")

root.wm_attributes ('-transparentcolor', 'black')

root.mainloop()
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Eliizahh
  • 23
  • 3
  • 2
    what is your question exactly? Also you don't need to add anything to display an image without a background or what do you mean by that? or do you want the image to shape the window? – Matiiss Oct 12 '21 at 18:19
  • Asking for library recommendations is off-topic on Stackoverflow. – Bryan Oakley Oct 12 '21 at 18:31
  • @Matiiss Seeing my question from a reader's perspective would no longer be a recommendation, it would be a problem since tkinter does not provide a tool to make the image as Matiiss says, that the image shapes the window. – Eliizahh Oct 12 '21 at 18:37
  • Does this answer your question? [How to make a tkinter window rounded?](https://stackoverflow.com/questions/68845235/how-to-make-a-tkinter-window-rounded) – Matiiss Oct 12 '21 at 18:41
  • @Eliizahh yes, I just linked a question that shows how to do it, except for those images you would need to remove the background first, you can do that either manually using some photo editor, use an online service or if you really want to do it programmatically you could use the python imaging library ([PIL](https://pillow.readthedocs.io/en/stable/), `pip install pillow` but that would be unnecessary and suggesting such libraries is off-topic) – Matiiss Oct 12 '21 at 19:12
  • @Matiiss Reading the link you provided a bit more, I realized that you suggest ```wm_attributes ('-transparentcolor', 'black')```. But I'm afraid this solution is not cross-platform, apart from testing a bit I realized that it doesn't work with ```wm_attributes```. Isn't there a library that removes colors from an image without resorting to ```wm_attributes```? – Eliizahh Oct 12 '21 at 19:49
  • @Eliizahh what do you mean by that? first of you can use any color there not just black and also that color is not meant to remove the background of picture, it makes that color to be transparent on the root and although it also affects the picture, for the picture you have to yourself remove the background and yes, the name of the attribute is a bit different on other OS, for example on Linux it might be `'-splash'` but I am not sure – Matiiss Oct 12 '21 at 19:55
  • @Matiiss I understand the point and it is correct. But if you use, ```attributes ('-transparentcolor')``` and ```overrideredirect(True)```, and you know that your program will be distributed on different computers. Will it work without any problem? Or do I need another solution that is cross-platform? – Eliizahh Oct 13 '21 at 04:30
  • @Eliizahh I am pretty sure that `overrideredirect` flag is cross-platform, `'-transparentcolor'`, on the other hand, is not but there is an alternative for the other OS that functions the same, then you can use the `platform` module (built-in) to set the attribute differently depending on the OS using some if statements. Also you can't be sure whether it works on other OS unless you can test it on that other OS anyways, besides, if you don't have acceess to the other OS, you may not be able to even convert your file to a runnable application by that OS. – Matiiss Oct 13 '21 at 04:58

0 Answers0