establecer una imagen de fondo con tkinter [cerrado]

I have a gui with buttons, labels, text boxes, etc., and I wanted to place it all on a background image (say Elvis's face), like a desktop. I'd like it so that the text boxes and labels don't block out the image, but rather the text sits on the image and the image remains fully visible. Any suggestions?

preguntado el 03 de mayo de 12 a las 20:05

1 Respuestas

OK, so given a few caveats, I've made this work. First off, I didn't bother trying to make it work with ttk widgets as I'm not up to speed on styles yet. Secondly, I know this will work for Windows and pretty sure it won't work on other platforms. All that said, the trick is to pop a Toplevel window (named overlay in my code) on top of your image configure it to have a transparent color (which is apparently only possible in Windows Tk) then put your widgets on the overlay and set their background to your transparent color (trans_color in my code). I also trap the root's <Configure> event to keep the overlay in place. For the image, I just right clicked your profile image and saved it to disk (naming it 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()

contestado el 03 de mayo de 12 a las 23:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.