当在eclips之外运行时,程序不能完全工作

2024-10-08 18:22:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小python应用程序,它使用pyttsx进行文本到语音转换。在

工作原理: 只要说出剪贴板上有什么。在

该程序在eclipse中按预期工作。但如果继续命令行.exe只有当剪贴板上的文本太大(一些段落)时,它才会部分工作。为什么?在

从cmd运行时,它会打印语句,但实际的“对话”不起作用(如果剪贴板文本太大

下面是一个实际执行对话的程序部分:可以看出,“谈话”部分是在线程中处理的。在

def saythread(queue , text , pauselocation, startingPoint):
    saythread.pauselocation = pauselocation
    saythread.pause = 0 
    saythread.engine = pyttsx.init()      
    saythread.pausequeue1 = False

    def onWord(name, location, length):      
        saythread.pausequeue1  = queue.get(False) 
        saythread.pause = location
        saythread.pauselocation.append(location)

        if saythread.pausequeue1 == True :
            saythread.engine.stop()

    def onFinishUtterance(name, completed):
        if completed == True:
            os._exit(0)            

    def engineRun():

        if len(saythread.pauselocation) == 1:
            rate = saythread.engine.getProperty('rate')
            print rate 
            saythread.engine.setProperty('rate', rate-30)
        textMod = text[startingPoint:]

        saythread.engine.say(text[startingPoint:])
        token = saythread.engine.connect("started-word" , onWord )
        saythread.engine.connect("finished-utterance" , onFinishUtterance )
        saythread.engine.startLoop(True)

    engineRun()

    if saythread.pausequeue1 == False:
        os._exit(1) 


def runNewThread(wordsToSay, startingPoint):    
    global queue, pauselocation
    e1 = (queue, wordsToSay, pauselocation, startingPoint)
    t1 = threading.Thread(target=saythread,args=e1)
    t1.start()

#wordsToSay = CLIPBOARD CONTENTS
runNewThread(wordsToSay,0)

谢谢

编辑:我检查了比python使用的版本是相同的2.7。用于在cmd中运行程序的命令:python d:\python\play\speech\speechplay.py


Tags: text文本程序falseifratequeuedef
3条回答

结果发现我的系统没有正确设置pythonpath。 编辑:原来pythonpath不是问题所在。我不知道有什么问题。啊啊啊啊啊啊啊啊啊啊啊啊啊啊

事实上,eclipse本身使用命令行命令来启动它的应用程序。在

您应该检查eclipse发出了什么命令来启动程序。它可能有点冗长,但您可以从那里开始测试哪些是必需的,哪些不是

通过运行该程序,然后在调试窗口中选择输出,可以找到eclipse使用的命令行。右键单击它,选择属性,就完成了。在

如果没有调试窗口,可以打开它window/show view/(其他可能)/debug。在

已检查问题是否不在从剪贴板读取文本的代码中。在

您应该检查您的eclipse设置是否为项目指定了eclipse外部不存在的自定义环境变量。尤其是:

  • PYTHONPATH(以及您的程序在设置中可能依赖的其他项目)
  • 路径

使用

import os
print os.environ['PATH']
print os.environ['PYTHONPATH']

在程序开始时比较两个设置。在

其他文体建议:

  • 不要使用os._exit,而要使用sys.exit(在调用os.fork之后,只应在子进程中使用os._exit,这在Windows上不可用)

  • 我认为threading.Eventqueue.Queue

    更合适
  • 我将使用子类方法来处理线程和方法,而不是使用具有内部函数的函数

例如:

^{pr2}$

相关问题 更多 >

    热门问题