如何克服 PySimpleGUI 中的 "NoneTypeObject" 错误?
How to overcome "NoneTypeObject" error in PySimpleGUI?
下面是我的 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
开始说第一个字符后出现此错误。
有问题
- 线程的第一个参数是函数
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()
下面是我的 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
开始说第一个字符后出现此错误。
有问题
- 线程的第一个参数是函数
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()