Skip to content
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

Closed
4 of 7 tasks
elvis-brandao opened this issue May 5, 2021 · 7 comments
Closed
4 of 7 tasks
Labels
duplicate This issue or pull request already exists question Further information is requested

Comments

@elvis-brandao
Copy link

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]

  • Searched main docs for your problem www.PySimpleGUI.org
  • Looked for Demo Programs that are similar to your goal Demos.PySimpleGUI.org
  • If not tkinter - looked for Demo Programs for specific port
  • For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

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 ...

@jason990420
Copy link
Collaborator

Duplicate !

#364
#649
#2172
#3003
#3179
#3736

@jason990420 jason990420 added duplicate This issue or pull request already exists question Further information is requested labels May 5, 2021
@PySimpleGUI
Copy link
Owner

Discussed all over the place. There is even a weird hack that is released as a demo program.

image

image

@3hy-X1OS
Copy link

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?

@jason990420
Copy link
Collaborator

Maybe you can try this: #4972 (comment)

@34j
Copy link

34j commented May 30, 2023

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)

@jason990420
Copy link
Collaborator

it does not display very beautifully.

IMO, it say nothing and cannot help us to understand what your question.

@34j
Copy link

34j commented May 30, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants