如何在 PySimpleGUI 中显示 window 中的图像?

How to show images in window in PySimpleGUI?

我正在使用 PySimpleGUI,我想在 PySimpleGUI 的 Window 中显示图像。基本上,首先我将我的句子转换成字符,现在我只想在 window 中显示该字符的图像并说出该字符。 下面是我的代码:

from tkinter.constants import TRUE
import PySimpleGUI as sg
from PySimpleGUI.PySimpleGUI import Window
import pyttsx3
import time
import _thread

first_coloumn = [
    [sg.LBox([], size=(20,10), key='-FILESLB-')],
    [sg.Input(visible=False, enable_events=True, key='-IN-'), sg.FilesBrowse()],
    [sg.Text('Enter what you wanna teach : ')],
    [sg.Input(key='-inp-')],
    [sg.Button('Reads'),sg.Button('Pause'),sg.Button('Slow'),sg.Button('Fast'),sg.Button('Stop'),sg.Button('Repeat'),sg.Button('Exit')]
]

image_viewer_coloumn = [
     [sg.Text("Image Viewer")],
     [sg.Image(key='-IMAGE-')],
 ]

layout = [
    [
        sg.Column(first_coloumn),
        sg.VSeparator(),
        sg.Column(image_viewer_coloumn),
    ]
 ]


window = sg.Window("Narrator",layout)
engine = pyttsx3.init()
words = []
chars = []
a = 0.5
stop = False
def splits(sentence):
    return list(sentence)
def speak(a):
    global stop
    for i in range (len(chars)):
        if stop:
            break   
        elif chars[i]=='A' or chars[i]=='a':
            filepath = "D:\Office-Work\Chars\A.png"
            window['-IMAGE-'].update(filepath=filepath)
            engine.say(chars[i])
            time.sleep(a)
            engine.runAndWait()
while TRUE:
    event,values = window.read()
    
    if event == 'Reads':
        out = values['-inp-']
        if out:    
            chars = splits(out)
            stop = False
            _thread.start_new_thread(speak, (a,))

我收到以下错误:

update() got an unexpected keyword argument 'filepath'
  File "C:\Users\Kashi\OneDrive\Desktop\PySimpleGUI\a.py", line 45, in speak
    window['-IMAGE-'].update(filepath=filepath)

sg.Image定义为

def __init__(self, source=None, filename=None, data=None, background_color=None, size=(None, None), s=(None, None), pad=None, p=None, key=None, k=None, tooltip=None,  right_click_menu=None, expand_x=False, expand_y=False, visible=True, enable_events=False, metadata=None):

您会发现 filepath 没有选项,另一个问题是不在主线程中时不更新 GUI。

...
        elif chars[i]=='A' or chars[i]=='a':
            filepath = "D:\Office-Work\Chars\A.png"
            """
            window['-IMAGE-'].update(filepath=filepath)    # unexpected keyword argument 'filepath'
            window['-IMAGE-'].update(filename=filepath)    # correct keyword, but not update GUI here
            """
            window.write_event_value("Update Image", filepath)    # Generate an event when not in main thread.
...
while TRUE:
    event,values = window.read()
...
    if event == "Update Image":
        window['-IMAGE-'].update(filename=filepath)
...

不应在其他线程函数中更新 GUI speak