0

I'm trying to find a solution to make the background of my image-button transparent. I can come up with a solution with create_image but i need to be able to add text to the buttons. It looks like the image png transparency is supported but the grey you can see is from the button background itself.

Here is an example code:

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()

bgImage = ImageTk.PhotoImage(Image.open("assets/node_button.png"))
button = tk.Button(text='TEST BUTTON', command=lambda: print(f'pressed button'), image=bgImage, 
                   borderwidth=0, bg='white', highlightthickness=0, relief='flat', 
                   activebackground='white', compound='center', fg='white')

button_id = canvas.create_window(1, 1, anchor='nw', 
                                      window=button, width=100, height=40)
button.bind('<B1-Motion>', quitGame)


window.mainloop()

Anyone knows a way around this problem?

user9468014
  • 479
  • 2
  • 6
  • 20
  • What do you want to see when looking through the transparent button; the back of your `Tk` window, or the whatever window is underneath it? – Sylvester Kruin Feb 08 '22 at 17:23
  • 1
    sorry for the unclearness. Just the button should be transparent – so the back of the TK window – user9468014 Feb 08 '22 at 17:32
  • You can try [this answer](https://stackoverflow.com/a/39271423/16775594), but it doesn't work for some people for some reason. And according to [this other answer](https://stackoverflow.com/a/10461175/16775594) to the same question, transparent widgets unfortunately aren't possible at all with `tkinter`. – Sylvester Kruin Feb 08 '22 at 17:45
  • thank you. Yes wm_attributes "-transparentcolor" is just working for Windows user apparently – user9468014 Feb 08 '22 at 19:27
  • You can use `create_image()` and `create_text()` together to simulate what you want. – acw1668 Feb 09 '22 at 00:25

0 Answers0