问题:使用字典中的 PNG 文件在 PySimpleGUI (Python) 中的 Image 小部件中显示
class Image
定义为:
class Image(Element):
def __init__(self, filename=None, data=None, ...):
"""
:param filename: (str) image filename if there is a button image.
GIFs and PNGs only.
:param data: Union[bytes, str] Raw or Base64 representation of the image
to put on button.
Choose either filename or data
你可以做:
import PySimpleGUI as sg
import os
cwd = os.getcwd()
fname = 'image1.png'
with open('{}/{}'.format(cwd, fname)) as fh:
image1 = fh.read()
[sg.Image(data=image1, key='key1', size=(5, 6))]
像这样的东西应该可以工作(假设有两个图像:)image1, image2
:
import PySimpleGUI as sg
# All the stuff inside your window.
layout [
[sg.Image(data=image1, key='__IMAGE__', size=(5, 6))]
]
# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event in (None, 'Cancel'): # if user closes window or clicks cancel
break
window.Element('_IMAGE_').Update(data=image2)
window.close()