4

I have just started working with the PySimpleGUI framework with the Tkinter port and I don't understand how an image can be inserted as a background for a window in a program.

There is no argument or parameter associated with adding a background image for the Window component.

My code:

import PySimpleGUI as sg
        
layout1 = [[sg.Text("What File Type do you want to generate?")],
           [sg.Checkbox("PowerPoint Presentation", auto_size_text=True)],
           [sg.Checkbox("PDF", auto_size_text=True)]]
        
window = sg.Window("Demo", layout1)
        
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
        
window.close()

DapperDuck
  • 2,728
  • 1
  • 9
  • 21
Anmol Saksena
  • 91
  • 2
  • 10

1 Answers1

1

In the latest version of PySimpleGUI, it's very simple. If you look at the screenshots on PySimpleGUI website https://pysimplegui.readthedocs.io/en/latest/screenshots_demos/ you will see a window with the Milky Way Galaxy set as the background.

Here is the code on how to do it. https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Window_Background_Image.py

However, the code embeds the image data into the file. I prefer not to do it that way. I prefer to just call it by the filename. If you prefer to do it that way too, just change:
background_layout = [ title_bar('This is the titlebar', sg.theme_text_color(), sg.theme_background_color()), [sg.Image(data=background_image)]]
and remove background_image = b'... at the bottom of the file

to

background_layout = [title_bar('This is the titlebar', sg.theme_text_color(), sg.theme_background_color()), [sg.Image(r'background.png')]]

Hope that helps :)

wildernessfamily
  • 405
  • 4
  • 11
  • Unfortunately, this solution using coupled windows doesn't work in MacOS in this form (`transparent_color` cannot be used). There are also some side effects in Windows; for example, if a part of your window gets behind another window, then clicking it will only bring to front the front part of your window pair while the background stays behind the other window. To (partially) deal with the latter issue, you can add `top_window.bind('', '+DOUBLE_CLICK+')` and on that event, you can bring to front both window_background and top_window. – Lenka Čížková May 21 '21 at 21:33
  • I use PySimpleGUI on a Mac Book Pro. I haven't tried using transparent color on my windows. There are some things that I find buggy when using on a Mac. If I need more features I use KIVY. But, for simple apps that need to be done fast, PySimpleGUI has always worked for me. I have run into the problem where a window falls behind another window. Especially with popups. I use `keep_on_top=True' and that solves the problem. Most of my popups or are either okay popups, cancel popups, etc. The end-user reads the popup clicks the button. Using keep_on_top I've never had a compalint. – wildernessfamily May 22 '21 at 22:11
  • I agree that for simple popups, using `keep_on_top=True` is a good solution in most cases (especially because you mostly want user's interaction in that moment). But for an app whose main window takes about half the screen, it's not optimal. – Lenka Čížková May 23 '21 at 12:02
  • Absolutely, I totally agree. When I need something more sophisticated then a “simple” :) app I use KIVY. KIVY 2.0 is becoming pretty simple too. And KIVYMD is just amazing and so well documented. I may stop using PySimpleGUI all together. Right now I still use it to make quick GUI API apps, and Raspberry Pi Apps. – wildernessfamily May 24 '21 at 15:11
  • Thanks for tip, we may look at KIVY at some moment for our project. For now, I feel I know PySimpleGUI better and better each week and have found workarounds for most problems we have faced so far, including having a background image as mentioned above, just not having the top with a transparent colour but instead, the whole top window has alpha set to 0.7 or 0.8 and, of course, the theme matching the background image. After all, we quite like the effect :) – Lenka Čížková May 24 '21 at 23:50