Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting an image as background in PySimpleGUI window

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

like image 811
Anmol Saksena Avatar asked Dec 06 '25 06:12

Anmol Saksena


1 Answers

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

like image 194
wildernessfamily Avatar answered Dec 07 '25 21:12

wildernessfamily



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!