Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create transparent widgets using Tkinter?

Tags:

python

tkinter

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'?

like image 614
Ashok Avatar asked Jun 11 '13 08:06

Ashok


People also ask

How do I make a transparent background in Tkinter?

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.

Can you make a canvas transparent Tkinter?

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.


1 Answers

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.

like image 82
Justin S Barrett Avatar answered Sep 25 '22 23:09

Justin S Barrett