【问题标题】:How to show images in window in PySimpleGUI?如何在 PySimpleGUI 的窗口中显示图像?
【发布时间】:2021-11-20 11:19:56
【问题描述】:

我正在研究 PySimpleGUI,我想在 PySimpleGUI 的窗口中显示图像。基本上,首先我将我的句子转换为字符,现在我想简单地在窗口中显示该字符的图像并说出哪个字符。 以下是我的代码:

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)

【问题讨论】:

    标签: python user-interface pysimplegui


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 1970-01-01
      • 2017-08-19
      • 2011-05-05
      • 2022-12-11
      • 2021-11-25
      • 1970-01-01
      • 2022-01-26
      • 2021-04-06
      相关资源
      最近更新 更多