Home > Back-end >  How to show images in window in PySimpleGUI?
How to show images in window in PySimpleGUI?

Time:09-29

I'm working on PySimpleGUI and I want to show images in the Window of PySimpleGUi. Basically, first I'm converting my sentence into characters and now I want to simply show an image of that character in the window and speak which is the character. Below is my code:

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

and I'm getting the following error:

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)

CodePudding user response:

sg.Image defined as

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

You can find there's no option for filepath, another issue is not to update GUI when not in main thread.

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

Should not update GUI in other thread function speak.

  • Related