I wanted to create an app which has an image as its background. But when I add a Label
over the image, the label had a white background.
Is there a way to set the Label
widget's background color to 'transparent'?
To create a transparent background, we need to use the -alpha argument in the attributes() method. The alpha is Used for transparency. If the transparency value is 0.0 it means fully transparent, 1.0 means fully opaque The range is [0.0,1.0]. This isn't supported on all systems, Tkinter always uses 1.0.
A transparent ColorKey can be set with SetLayeredWindowAttributes while we just use LWA_COLORKEY the alpha parameter has no use to us. Important note: After defining a transparent colorkey, everything in that canvas with that color will be transparent.
I'm not aware of a way to make Label backgrounds transparent. One alternative is to use a Canvas widget as the base for the whole thing, then use the create_image
method to add the background image, and create_text
to make the text labels. It will be a bit more work, but the text should render without a background on top of the image. (Admittedly I have very little experience with the Canvas widget, so I'm speaking more from theory than experience, but it's worth a try.)
If you don't have a good Tkinter reference, I highly recommend this one made by New Mexico Tech. It's available as a downloadable PDF as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With