【问题标题】:How to overcome "NoneTypeObject" error in PySimpleGUI?如何克服 PySimpleGUI 中的“NoneTypeObject”错误?
【发布时间】:2021-11-20 09:26:02
【问题描述】:

下面是我的 PySimpleGUI 代码。我想通过“拆分”功能将我的文本转换为字符。然后我想在按下“阅读”按钮后一个接一个地听这些字符。我有一个按钮“停止”来停止执行该功能。请检查此代码并告诉我为什么会出错。谢谢你

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

layout = [
    [sg.Text('Enter what you wanna teach : ')],
    [sg.Input(key='-inp-')],
    [sg.Button('Reads'),sg.Button('Stop'),sg.Button('Exit')]
]

window = sg.Window("Narrator",layout)
engine = pyttsx3.init()
chars = []
a = 0.5
stop = False
def splits(sentence):
    return list(sentence)
def speak(a):
    global stop
    for i in range (len(chars) and not stop):   
        engine.say(chars[i])
        time.sleep(a)
        engine.runAndWait()
while TRUE:
    event,values = window.read()
    
    if event == 'Reads':
        out = values['-inp-']
        chars = splits(out)
        stop = False
        _thread.start_new_thread(speak(a), ())
    elif event == 'Stop':
        stop = True
    elif event == 'Exit' or event == sg.WIN_CLOSED:
        break

错误是:

Exception ignored in thread started by: <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at 0x000001AF5370C400>
Traceback (most recent call last):
  File "c:\Users\Kashi\.vscode\extensions\ms-python.python-2021.9.1246542782\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 1054, in __call__
    ret = self.original_func(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable

在它开始说出第一个字符后出现此错误。

【问题讨论】:

    标签: python user-interface pysimplegui


    【解决方案1】:

    有问题

    • 线程的第一个参数是函数speak,而不是speak(a)的结果。
    • for i in range (len(chars) and not stop):,它将是for i in range(0):for i in range(1):

    在此处更新代码

    import time
    import _thread
    
    import pyttsx3
    import PySimpleGUI as sg
    
    
    def splits(sentence):
        return list(sentence)
    
    def speak(a):
        global stop
        for i in range(len(chars)):
            if stop:
                break
            engine.say(chars[i])
            time.sleep(a)
            engine.runAndWait()
    
    layout = [
        [sg.Text('Enter what you wanna teach : ')],
        [sg.Input(key='-inp-')],
        [sg.Button('Reads'),sg.Button('Stop'),sg.Button('Exit')]
    ]
    
    window = sg.Window("Narrator",layout)
    engine = pyttsx3.init()
    chars = []
    a = 0.5
    stop = False
    
    while True:
    
        event, values = window.read()
    
        if event in (sg.WIN_CLOSED, "Exit"):
            break
        elif event == 'Reads':
            out = values['-inp-'].strip()
            if out:
                chars = splits(out)
                stop = False
                _thread.start_new_thread(speak, (a,))
        elif event == 'Stop':
            stop = True
    
    window.close()
    

    【讨论】:

    • 谢谢你,伙计!它工作得很好。
    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多