簡體   English   中英

根下所有小部件繼承的python tkinter透明度

[英]python tkinter transparency inherited by all the widgets under root

我有一個透明的根窗口。 我還有一個標簽,它是根窗口的子窗口。 我注意到標簽也和根一樣透明。 下面是我正在使用的代碼:

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,並使標簽文本在屏幕上可見

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM