root下所有小部件继承的python tkinter透明度

问题描述 投票:0回答:1

我有一个透明的根窗口。我也有一个标签,它是根窗口的子级。我注意到标签也和根一样透明。下面是我正在使用的代码:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

def exit(event):
    root.destroy()

class TestApp:
    def __init__(self, parent):
        self.parent = parent
        self.label = tk.Label(self.parent, font=("Arial", 18, 'bold'),
                        width=30, fg="red")
        self.label.configure(text="Test message")
        self.label.pack()


if __name__ == "__main__":
    testApp = TestApp(root)

    root.bind("<Key>", exit)
    root.geometry("%sx%s" % (screen_width, screen_height))
    root.attributes('-alpha', 0.3)
    root.overrideredirect(True)
    root.lower()
    root.wm_attributes("-topmost", True)
    root.wm_attributes("-disabled", True)
    root.wm_attributes("-transparentcolor", "white")
    root.mainloop()

如果alpha的值是0.3(在root.attributes('-alpha',0.3)行中,则文本在屏幕上可见,但是如果值为0.0,则文本在屏幕上不可见) 。只想知道如何将root的透明度设置为0.0,并使标签文本在屏幕上可见

python tkinter
1个回答
0
投票
root.wm_attributes("-transparentcolor","white")

这将使您提供的颜色透明。如果要使root背景透明,则可以使用root.wm_attributes("-transparentcolor", root['bg'])。然后,无需设置root.attributes('-alpha', 0.3)

您的代码应为:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

def exit(event):
    root.destroy()

class TestApp:
    def __init__(self, parent):
        self.parent = parent
        self.label = tk.Label(self.parent, font=("Arial", 18, 'bold'),
                        width=30, fg="red")
        self.label.configure(text="Test message")
        self.label.pack()


if __name__ == "__main__":
    testApp = TestApp(root)

    root.bind("<Key>", exit)
    root.geometry("%sx%s" % (screen_width, screen_height))
    root.overrideredirect(True)
    root.lower()
    root.wm_attributes("-topmost", True)
    root.wm_attributes("-disabled", True)
    root.wm_attributes("-transparentcolor", root['bg'])
    root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.