Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm making a package for my python assistant and have found a problem.

Im importing the following program into the main script.

import os

def load() :
    def tts(name) :
        os.system("""PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(' """ + name + " ');"

how do i call the function into my program

ive tried :

import loadfile
loadfile.load().tts("petar")

and it didn't work

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
448 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are never supposed to expose a sub-function outside of its scope, in this case, the tts method outside load. It's actually imposible to access tts without exposing its reference outside of your load() method. I suggest you to rather use a class like this:

In loadfile.py:

import os

class LoadFile(object):
    def tts(self, name):
        os.system("""PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(' """ + name + " ');")

def load():
    return LoadFile()

On main code: import loadfile loadfile.load().tts("petar")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...