8

Is it possible to configure tkinter or ttk widgets (Label, Entry, Text) with a transparent background so that they can be placed in containers with custom background colors or on top of canvas graphics or images?

I'm also looking for a way to change the background color of a ttk Frame widget?

Do I need to use the new ttk Style objects to accomplish the above? (I'm new to tkinter/ttk and still trying to get my head around the proper way to do things).

martineau
  • 119,623
  • 25
  • 170
  • 301
Malcolm
  • 5,125
  • 10
  • 52
  • 75

1 Answers1

3

Except on OSX, what you want can't be done because all those widgets are drawn on real windows which are rectangular (unless you use some low-level tricks to chop holes out of the window concerned; not an approach I recommend even though I've got code – for Tcl/Tk, not Python/Tkinter admittedly – which does it). OSX is the exception because there widgets (mostly) aren't drawn on real windows, but rather use a lighter-weight system (because that's how OSX works).

You can tune the widgets to have less of a border than normal, which is OK if you're using a style that keeps them fundamentally square. Or if you're using a canvas for your “interesting” surface, you can build button-like things on the canvas which may well be better for what you're actually doing.

To change the background color of a Ttk frame, you'll have to apply a style to it. The best instructions on style creation I've seen so far are in the tkdocs style tutorial, which is where I go when I want to look up how to do these things. (Yes, the Ttk documentation ought to cover this itself, but it doesn't yet.)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • The answer ("it can't be done") may be correct, but the rationale (rectangular windows) is not. Regardless of the shape of the window, the foreground and background colors should, in principle, support transparency. If the Label widget supported a transparent background color (the empty string "") it should be possible to write a style that used that color for everything but text foreground, and the shape of the transparent elements wouldn't matter. – Dave Oct 30 '11 at 19:00
  • @Dave: Standard X11 doesn't support transparency in colors, no matter how much you think it “should” (but could theoretically do it for particular surfaces through use of OpenGL). With Windows, something could be done with `UpdateLayeredWindow` but nobody's ever actually contributed the code to do it. – Donal Fellows Jan 11 '12 at 14:39
  • I may be wrong, but it should be possible to essentially replace the widgets with images or gifs that have transparency where needed, and then disable window manager. You would have to animate pressing a button etc. however. – kippel Apr 24 '21 at 00:29