Is there a way to create transparent windows with

2019-02-16 11:27发布

问题:

I'm trying, ultimately, to create "oddly-shaped windows" with Python using the Tkinter module. But for now I will settle for being able to make the background transparent while keeping child widgets fully-visible.

I'm aware this is done with wxPython and some other modules, but I'm inquiring as to the limits of Tkinter.

Can Tkinter create a clear Canvas or Frame? Can it pack UI elements without a canvas or frame? Can individual UI elements be transparent?

Can it pass mouse-click locations back to the system for processing any windows below it in the Z stack?

回答1:

The option root.attributes('-alpha', 0.1) can be used to make a transparent window

from Tkinter import *
root = Tk()
root.attributes('-alpha', 0.3)
root.mainloop()

However in this case even the widgets on the root will inherit the transparency.

Update for Linux (Tested on Ubuntu)

The above code does not work on Linux machines. Here's an update that works on Linux.

from tkinter import Tk # or(from Tkinter import Tk) on Python 2.x
root = Tk()
root.wait_visibility(root)
root.wm_attributes('-alpha',0.3)
root.mainloop()

Not sure if this works on Windows.