Following code created about 4 to 5 years ago, it use methods `pack` and `place` to put image under window.
```
from io import BytesIO
from PIL import Image
import random
from PIL import Image, ImageDraw
import PySimpleGUI as sg
def image_to_data(im):
if im.mode == 'CMYK':
im = im.convert('RGB')
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
def make_background(window, im, main_frame):
global images
def find_frames(widget):
widgets = list(widget.children.values())
if isinstance(widget, (sg.tk.Frame, sg.tk.LabelFrame)):
widget.update()
x, y = widget.winfo_rootx() - x0, widget.winfo_rooty() - y0
width, height = widget.winfo_width(), widget.winfo_height()
new_im = im_.crop((x, y, x+width, y+height))
image = sg.tk.PhotoImage(data=image_to_data(new_im))
images.append(image)
label = sg.tk.Label(widget, image=image, padx=0, pady=0, bd=0, bg=bg)
label.place(x=0, y=0)
label.lower()
for widget in widgets:
find_frames(widget)
size = window.size
im_ = im.resize(size)
root = window.TKroot
widgets = list(root.children.values())
x0, y0 = root.winfo_rootx(), root.winfo_rooty()
frame = sg.tk.Frame(root, padx=0, pady=0, bd=0, bg=bg)
frame.place(x=0, y=0)
images = []
image = sg.tk.PhotoImage(data=image_to_data(im_))
images.append(image)
label = sg.tk.Label(frame, image=image, padx=0, pady=0, bd=0, bg=bg)
label.pack()
main_frame.Widget.master.place(in_=frame, anchor='center', relx=.5, rely=.5)
frame.lower()
frame.update()
for widget in widgets:
find_frames(widget)
bg = sg.theme_background_color()
background_image_file = 'c:/download/example.png'
w, h = size = 480, 360 # size of background image
im = Image.new("RGB", size, bg)
draw = ImageDraw.Draw(im)
for _ in range(100):
radius = random.randint(5, 15)
x = random.randint(radius, w - radius)
y = random.randint(radius, h - radius)
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
bounding_box = (x - radius, y - radius, x + radius, y + radius)
draw.ellipse(bounding_box, fill=(r, g, b))
frame = [
[sg.Input(size=(30, 1), expand_x=True),
sg.Button('Browse')],
[sg.Multiline('', expand_x=True, expand_y=True)],
[sg.Button('Exit')],
]
# Need only one frame here to move it to center of window
layout = [[sg.Frame('', frame, size=(350, 200), border_width=0, key='FRAME', background_color=bg)]]
location = sg.Window.get_screen_size()
window = sg.Window('Background Demo', layout, margins=(0, 0), grab_anywhere=True,
size=size, keep_on_top=True, finalize=True,
no_titlebar=True,
transparent_color=bg,
)
images = []
make_background(window, im, window['FRAME'])
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Cancel', 'Exit'):
break
print(event)
window.close()
```
[](https://i.sstatic.net/XRaneHcg.png)
Here's the example, but with tkinter code.
```python
import os
from io import BytesIO
from PIL import Image
import PySimpleGUI as sg
def image_to_data(im):
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
def make_background(window, file, main_frame):
global images
def find_frames(widget):
widgets = list(widget.children.values())
if isinstance(widget, (sg.tk.Frame, sg.tk.LabelFrame)):
widget.update()
x, y = widget.winfo_rootx() - x0, widget.winfo_rooty() - y0
width, height = widget.winfo_width(), widget.winfo_height()
new_im = im_.crop((x, y, x+width, y+height))
image = sg.tk.PhotoImage(data=image_to_data(new_im))
images.append(image)
label = sg.tk.Label(widget, image=image, padx=0, pady=0, bd=0, bg=bg)
label.place(x=0, y=0)
label.lower()
for widget in widgets:
find_frames(widget)
size = window.size
im_ = Image.open(file).resize(size)
root = window.TKroot
widgets = list(root.children.values())
x0, y0 = root.winfo_rootx(), root.winfo_rooty()
frame = sg.tk.Frame(root, padx=0, pady=0, bd=0, bg=bg)
frame.place(x=0, y=0)
images = []
image = sg.tk.PhotoImage(data=image_to_data(im_))
images.append(image)
label = sg.tk.Label(frame, image=image, padx=0, pady=0, bd=0, bg=bg)
label.pack()
main_frame.Widget.master.place(in_=frame, anchor='center', relx=.5, rely=.5)
frame.lower()
frame.update()
for widget in widgets:
find_frames(widget)
bg = sg.theme_background_color()
background_image_file = os.path.join(os.getcwd(), 'background.png')
size = (640, 480)
sg.set_options(dpi_awareness=True)
frame = [
[sg.Text('Fill in the length', justification='center', background_color='black', font='Helvetica 18')],
[sg.Input(justification='center', size=10, key='length'),
sg.Text('X', justification='center', background_color='black'),
sg.Input(justification='center', size=10, key='width')],
[sg.Button('OK', key='OK')],
[sg.Button('Exit', key='Exit')]
]
# Need only one frame here to move it to center of window
layout = [[sg.Frame('', frame, border_width=0, key='FRAME', background_color=bg)]]
location = sg.Window.get_screen_size()
window = sg.Window('Background Demo', layout, margins=(0, 0), grab_anywhere=True,
size=size, keep_on_top=True, finalize=True,
no_titlebar=True,
transparent_color=bg,
)
images = []
make_background(window, background_image_file, window['FRAME'])
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Cancel', 'Exit'):
break
print(event)
window.close()
```
[![enter image description here][1]][1]
[1]: https://i.sstatic.net/t6md1.png