0

我有一个带有按钮、标签、文本框等的 gui,我想把它全部放在背景图像上(比如猫王的脸),比如桌面。我希望文本框和标签不会遮挡图像,而是文本位于图像上并且图像保持完全可见。有什么建议么?

4

1 回答 1

1

好的,所以给出了一些警告,我已经完成了这项工作。首先,我没有费心尝试让它与 ttk 小部件一起工作,因为我还没有跟上样式的速度。其次,我知道这将适用于 Windows,并且很确定它不会在其他平台上运行。综上所述,诀窍是在图像顶部弹出一个顶层窗口(在我的代码中命名为叠加层),将其配置为透明色(这显然只能在 Windows Tk 中实现)然后将小部件放在叠加层上并设置他们的背景到你的透明颜色(我的代码中的 trans_color )。我还捕获了根的<Configure>事件以保持覆盖在适当的位置。对于图像,我只需右键单击您的个人资料图像并将其保存到磁盘(将其命名为 spice.png)。

from Tkinter import *
#from ttk import *
from PIL import Image, ImageTk

trans_color = '#FFFFFE'

root = Tk()
img = ImageTk.PhotoImage(Image.open('spice.png'))
img_label = Label(root, image=img)
img_label.pack()
img_label.img = img  # PIL says we need to keep a ref so it doesn't get GCed
root.update()
overlay = Toplevel(root)
print 'root.geo=', root.geometry()
geo = '{}x{}+{}+{}'.format(root.winfo_width(), root.winfo_height(),
    root.winfo_rootx(), root.winfo_rooty())
print 'geo=',geo
overlay.geometry(geo)
overlay.overrideredirect(1)
overlay.wm_attributes('-transparent', trans_color)
overlay.config(background=trans_color)

lbl = Label(overlay, text='LABEL')
lbl.config(background=trans_color)
lbl.pack()

def moved(e):
    geo = '{}x{}+{}+{}'.format(root.winfo_width(), root.winfo_height(),
        root.winfo_rootx(), root.winfo_rooty())
    overlay.geometry(geo)

root.bind('<Configure>', moved)

root.mainloop()
于 2012-05-03T22:29:58.580 回答