New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] It is possible change/put a image as background of the window? #4253
Comments
i am also trying to do this but i cant help but notice that the example above dosent come up as an actual window and will remove the background when alt tabing or swiching tabs in general can this be coded some other way to avoid these issues? |
Maybe you can try this: #4972 (comment) |
Since the program in #4972 (comment) was difficult to understand, I have refactored it somewhat, but unfortunately it does not display very beautifully. from __future__ import annotations
from io import BytesIO
from pathlib import Path
from PIL import Image
import PySimpleGUI as sg
_PHOTOIMAGE_STORE: list[sg.tk.PhotoImage] = []
"""A list of PhotoImage objects that are used in the background.
Without this, the elements will disappear immediately."""
def _image_to_bytes(image: Image) -> bytes:
with BytesIO() as output:
image.save(output, format="PNG")
data = output.getvalue()
return data
def set_background(window: sg.Window, file: Path | str | Image) -> None:
"""Set the background of the window to the image.
Parameters
----------
window : sg.Window
The window to set the background of.
file : Path | str | PIL.Image
The image to set as the background.
"""
size = window.size
root = window.TKroot
bg = sg.theme_background_color()
if isinstance(file, Image.Image):
original_img = file.resize(size)
else:
original_img = Image.open(file).resize(size)
del file, window, size
global _PHOTOIMAGE_STORE
def set_background_to_children_recursively(widget: sg.tk.Widget) -> None:
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()
img = original_img.crop((x, y, x + width, y + height))
photoimg = sg.tk.PhotoImage(data=_image_to_bytes(img))
_PHOTOIMAGE_STORE.append(photoimg)
label = sg.tk.Label(widget, image=photoimg, padx=0, pady=0, bd=0, bg=bg)
label.place(x=0, y=0)
label.lower()
for widget in list(widget.children.values()):
set_background_to_children_recursively(widget)
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)
photoimg = sg.tk.PhotoImage(data=_image_to_bytes(original_img))
_PHOTOIMAGE_STORE.append(photoimg)
label = sg.tk.Label(frame, image=photoimg, padx=0, pady=0, bd=0, bg=bg)
label.pack()
frame.lower()
frame.update()
for widget in list(root.children.values()):
set_background_to_children_recursively(widget) |
IMO, it say nothing and cannot help us to understand what your question. |
I am not asking, just pointing out that as you know, the background of some elements such as sg.Push, sg.Text, sg.Slider, etc. will remain monochromatic (due to the tk specification). |
Type of Issues:
Question
Operating System
Windows 10
Python version
3.9.2
PySimpleGUI Port and Version
Ports = tkinter
Your Experience Levels In Months or Years
<1y Python programming experience
<9y Programming experience overall
no, sparsely Have used another Python GUI Framework (tkinter, Qt, etc) previously (yes/no is fine)?
These items may solve your problem. Please check those you've done by changing - [ ] to - [X]
Description of Problem / Question / Details
My question is only the one I put in the text of the issue.
I was wondering if you can put a background image in the window with the buttons and elements above it.
I saw something similar in the Demo Programs, but I was wondering if there is another way to do this ...
The text was updated successfully, but these errors were encountered: