Brandon Nadeau
Brandon Nadeau

Reputation: 3706

How to make a tkinter Label background transparent?

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

Upvotes: 16

Views: 116569

Answers (4)

Mohammad Chenarani
Mohammad Chenarani

Reputation: 113

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

Upvotes: 1

Martol1ni
Martol1ni

Reputation: 4702

It is not supported with transparent backgrounds in Tk.

Upvotes: 7

Fatih1923
Fatih1923

Reputation: 2691

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

Upvotes: 4

lstdmi
lstdmi

Reputation: 137

i think it can help, all black will be transparent

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

Upvotes: 8

Related Questions