'How can I use transparent images in tkinter without png?
I am making a tkinter project for school. In the project, I need to use transparent images. In my school, the tkinter only supports GIFs so I cannot use a PNG. Moreover, only the modules that come with python 3 when installing are supported. We cannot download more. Is there a way to solve my problem?
Solution 1:[1]
tkinter can show the png transparence.
Example with a basic image.
import tkinter as tk
root = tk.Tk()
img = tk.PhotoImage(file='test.png')
can = tk.Canvas(root, width=300, height=300, bg='lightGrey')
can.grid()
can.create_rectangle(0, 0, 200, 200, fill='darkGreen')
can.create_rectangle(100, 100, 300, 300, fill='navy')
can.create_text(150, 150, text='TKINTER', font=('', 32), fill='orange')
can.create_image(150, 150, image=img)
root.mainloop()
You just need to save your image with the alpha parametre, with Gimp or a good image editor.
Solution 2:[2]
You can do this with GIFsicle, using the following options:
gifsicle -U --disposal=previous --transparent="#ffffff" -O2 pic.gif > pic_trans.gif
where pic.gif and pic_trans.gif are the source and destination file names, and #ffffff is the hex code of the color you want to make transparent (here, pure white).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Glorfindel |
| Solution 2 | scenox |


