Home > Back-end >  How to overcome "NoneTypeObject" error in PySimpleGUI?
How to overcome "NoneTypeObject" error in PySimpleGUI?

Time:09-29

below is my code for PySimpleGUI. I want to convert my text to characters through the "splits" function. And then I want to listen to those characters one after the other, after pressing the button "Reads". And I've one button "Stop" to stop execution of the function. Please check this code and tell me why I'm getting error. Thank You

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

The Error is :

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

I get this error after it starts speaking the first character.

CodePudding user response:

There're something wrong

  • first argument of thread is function speak, not the result of speak(a).
  • for i in range (len(chars) and not stop):, it will be for i in range(0): or for i in range(1):.

Update code here

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()
  • Related