[英]How to layout widgets in a Python Tkinter frame under a root TopLevel
[英]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.