Transparent Backgrounds on Buttons in Tkinter
Asked Answered
P

5

9

I have a Button with a button image but when it is in my window the background of the button clashes with the background of the window. It is a .png image but tkinter seems to want to keep the image as a quadrilateral by adding grey space. Is there a way to make the empty space of a button become transparent so that you are just left with the button image?

I am using Python 3.4.2 on Windows 8.

Privileged answered 24/4, 2015 at 21:24 Comment(6)
This could well be a .png image issue. Try changing it to .gif (which supports transparent backgrounds) first, and let me know if it doesn't work.Quirk
PNG supports transparent backgrounds, and a .png imported into tkinter from PIL preserves transparency.Outcross
Nope, I have changed the file to a gif but the same thing is happening.Privileged
I have found a semi-useful page. It outlines how to make a transparent background on a label but unfortunately this method doesn't work with a button. Here is the link: #19080999Privileged
I think the issue might be that, while the image is transparent, the Button itself is not, so the grey you're seeing is the Button behind the image.Outcross
Is there a way to stop that then?Privileged
D
5

To create an image that supports .png transparency, you have to create a Canvas and then create an image object using the canvas .create_image() feature. Then bind an event to the canvas image using .tag_bind().

For example:

import tkinter as tk
from PIL import Image, ImageTk

def quitGame(event):
    window.destroy()

window = tk.Tk()
window.geometry("500x500")

canvas = tk.Canvas(window, width = 300, height = 300)
canvas.pack()

#creating background
bgImage = ImageTk.PhotoImage(Image.open("Images/background.png")) 
bg = canvas.create_image(0, 0, image=bgImage, anchor=tk.NW)

#creating button which supports png transparency
quitImage = ImageTk.PhotoImage(Image.open("Images/quitImage.png"))
quitButton = canvas.create_image(50, 50, image=quitImage)
canvas.tag_bind(quitButton, "<Button-1>", quitGame)

window.mainloop()
Dugas answered 18/11, 2021 at 18:25 Comment(1)
There's really no need to use the PIL to do this because tkinter.PhotoImage can read .png as well as .gif, .pgm, and .ppm formatted files. See my answer to a related post which illustrates this (but is otherwise based on yours).Dawna
R
1

The solution is a bit tricky as you need to go around using PIL but works. Just solved it after 2h of fighting.

You need to use PIL as an image loader and then pass the image to tkinter BUT with the usage of the root (main tk.Tk() class object) - without that the image won't be visible as it's gone in the garbage collector, only image's space remains. Simplified code below:

import tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()

button = tk.Button(self.left_menu)
button_load = Image.open('assets/search.png')
root.button_img = ImageTk.PhotoImage(button_load)
button.config(image=root.button_img)

button_1.pack(side='top')
Reunion answered 26/2, 2021 at 18:1 Comment(1)
I don't see this as addressing the OP's question at all.Dawna
H
0

If you are using a .png with import PIL python is supporting the transparency.

However, within tkinter the .Button widget does not support transparency.

So what you will have is transparent image on top of a solid background.

If you are a windows user, your best bet is this solution:

Transparent background in a Tkinter window

Hockenberry answered 15/9, 2016 at 11:22 Comment(0)
I
0

I had same issue that led me here. See image below. I thought my .png transparency was not working, but the png transparency was fine. The grey space I was seeing was the button background. By making the button background the same as the frame background, I got this desired result for the button.

The only change to code I made was adding bg=bg_menubox to the button creation as shown below. bg_menubox is the blue color seen in the background of the menubox frame where the button is created. Top part of image is without the bg=bg_menubox, the lower example is with.

Here's the relevant line of code:

btn_open = tk.Button(menuframe,image=image_open, command=open_midi, bg = bg_menubox)

adding bg parameter to button

Ibnsaud answered 11/4, 2023 at 20:35 Comment(0)
B
0

I have only been able to accomplish this using customtkinter.

In this example the button will be a transparent lizard image:

import tkinter as tk
import customtkinter as ctk
from PIL import Image

root = tk.Tk()

lizard_image = ctk.CTkImage(Image.open("lizard.png"), size=(50, 50))

lizard_button = ctk.CTkButton(root, 
                           image=lizard_image, 
                           fg_color='transparent',
                           text='')
lizard_button.pack()

tk.mainloop()

text='' is necessary, CTkButton will be displayed on the button without it.

Result examples:

Button:

button

Hovering over button:

hovering over button

Bullpen answered 7/4 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.