15

I have a window with a label as my frame. I did this because i wanted an image in the background. But now im having trouble with the other labels i have used. The other labels i have used to actually labeled things dont have a transparent background. Is there a way to make the background of these labels transparent?

import Tkinter as tk

root = tk.Tk()
root.title('background image')

image1 = Tk.PhotoImage(file='image_name.gif')

# get the image size
w = image1.width()
h = image1.height()

# make the root window the size of the image
root.geometry("%dx%d" % (w, h))

# root has no image argument, so use a label as a panel
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')

# put a button/label on the image panel to test it
label1 = tk.Label(panel1, text='here i am')
label1.pack(side=Top)

button2 = tk.Button(panel1, text='button2')
button2.pack(side='top')

# start the event loop
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Brandon Nadeau
  • 3,568
  • 13
  • 42
  • 65

4 Answers4

10

i think it can help, all black will be transparent

root.wm_attributes('-transparentcolor','black')
lstdmi
  • 133
  • 1
  • 2
  • 6
    Use `root.wm_attributes('-transparentcolor', root['bg'])` to make the default color transparent. – Nae Feb 15 '18 at 20:35
  • 3
    This would be a great solution if it only made the colored widget transparent, but it seems to make the colored widget and all lower widgets transparent. If you could make a lifted widget transparent and see the widgets lower than it, that would be great. Any idea how to make that happen? – GaryMBloom Feb 05 '20 at 03:58
  • 1
    Well this works like a charm making a specific color transparent but there are 2 major problems: a) Like Gary02127 said it makes all widgets transparent but not that it seems like (at least on Windows 10) it also makes all areas with the specified color permable too. – Nummer_42O Mar 09 '20 at 11:56
  • 5
    This doesn't work. I have tried it, and it does not accept `-transparentcolor` as a first parameter. – cs1349459 Jul 21 '20 at 16:37
7

It is not supported with transparent backgrounds in Tk.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Martol1ni
  • 4,684
  • 2
  • 29
  • 39
5

If you are working with images and putting text onto them, the most convenient way is - I think - utilizing Canvas widget.

tkinter Canvas widget has methods as .create_image(x, y, image=image, options) and .create_text(x, y, text="Some text", options).

Fatih1923
  • 2,665
  • 3
  • 21
  • 28
2

use this :

from tkinter import *

main=Tk()
photo=PhotoImage(file='test.png')
Label(main,image=photo,bg='grey').pack()
#your other label or button or ...
main.wm_attributes("-transparentcolor", 'grey')
main.mainloop()

this work if use bg='grey' you can change it in line 7

good luck :)

  • 2
    _tkinter.TclError: bad attribute "-transparentcolor": must be -alpha, -fullscreen, -modified, -notify, -titlepath, -topmost, or -transparent not working – exec85 Sep 04 '20 at 10:48
  • 13
    This is interesting, but it cut a hole in the entire app with the desktop visible behind the window. – codingCat Mar 29 '21 at 19:10
  • It worked for me, but the problem is that a pixelated grey outline appears around the text or image for some reason – 5alidshammout Aug 13 '23 at 04:48