在 PySimpleGUI 中插入图像作为背景 window

Inserting an image as background in PySimpleGUI window

我刚刚开始使用带有 Tkinter 端口的 PySimpleGUI 框架,我不明白如何在程序中插入图像作为 window 的背景。

没有与为 Window 组件添加背景图像关联的参数。

我的代码:

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

在最新版本的PySimpleGUI 中,非常简单。如果您查看 PySimpleGUI 网站 https://pysimplegui.readthedocs.io/en/latest/screenshots_demos/ 上的屏幕截图,您会看到 window 将银河系设置为背景。

这是有关如何操作的代码。 https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Window_Background_Image.py

但是,代码将图像数据嵌入到文件中。我不想那样做。我更喜欢用文件名来调用它。如果您也喜欢这样做,只需更改:
background_layout = [ title_bar('This is the titlebar', sg.theme_text_color(), sg.theme_background_color()), [sg.Image(data=background_image)]]
并删除文件底部的 background_image = b'...

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

希望对您有所帮助:)