2011-08-10 83 views
2

我有一个小型的python应用程序,它使用pyttsx来处理某些文本到语音。在eclipse外部运行时,程序无法完全运行

它是如何工作的: 只需说出剪贴板中的任何内容。

该程序在eclipse中按预期工作。但是,如果在cmd.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

+0

“run party”是什么意思?只是处理剪贴板中的文本的一部分,还是其他内容? –

+0

它将文本打印到标准输出,但不'说'任何东西 –

回答

0

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

您应该检查eclipse为启动程序而给出的命令。它可能有点冗长,但你可以从那里开始测试什么是必要的,什么不是。

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

如果你没有调试窗口,你可以打开窗口/显示视图/(其他可能)/调试。

+0

ok eclipse正在使用'python.exe -u file.py'。所以我尝试了它,但得到了相同的结果 –

+0

我想这两个exe和file.py的路径?如果您完全复制粘贴,会发生什么情况? – Peter

+0

是的,路径相同。我粘贴的eclipse编码完全。 –

2

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

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

  • PYTHONPATH(还有更多的项目上,你的程序也会取决于你的设置)
  • PATH

使用

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

在程序的开头比较这两个设置。

其它文体建议:

  • 不使用os._exit,喜欢sys.exit(你只应该在一个子进程使用os._exitos.fork一个电话,这是不是适用于Windows后)

  • 我认为threading.Event会比queue.Queue更合适

  • 我会使用一个subc该线程与方法,而不是一个功能姑娘的做法与内部功能

例如:

import threading 
import sys 
import pyttsx 

class SayThread(threading.Thread): 

    def __init__(self, queue, text, pauselocation, startingPoint, debug=False): 
     threading.Thread.__init__(self) 
     self.queue = queue 
     self.text = text 
     self.pauselocation = pauselocation 
     self.startingPoint = startingPoint 
     self.pause = 0 
     self.engine = pyttsx.init(debug=debug) 
     self.pausequeue1 = False 

    def run(self): 
     if len(self.pauselocation) == 1: 
      rate = self.engine.getProperty('rate') 
      print rate 
      self.engine.setProperty('rate', rate-30) 
     textMod = self.text[self.startingPoint:] 
     self.engine.say(self.text[self.startingPoint:]) 
     self.engine.connect("started-word", self.onWord) 
     self.engine.connect("finished-utterance", self.onFinishUtterance) 
     self.engine.startLoop(True) 
     if self.pausequeue1 == False: 
      sys.exit(1) 

    def onWord(self, name, location, length): 
     self.pausequeue1 = self.queue.get(False) 
     self.pause = location 
     self.pauselocation.append(location) 
     if self.pausequeue1 == True : 
      self.engine.stop() 

    def onFinishUtterance(self, name, completed): 
     if completed == True: 
      sys.exit(0) 


def runNewThread(wordsToSay, startingPoint): 
    global queue, pauselocation 
    t1 = SayThread(queue, wordsToSay, 
          pauselocation, startingPoint) 
    t1.start() 

#wordsToSay = CLIPBOARD CONTENTS 
runNewThread(wordsToSay,0) 
-2

原来PYTHONPATH未正确设置我的系统上。 编辑:原来pythonpath不是问题。我不知道有什么问题。 arghhhhhhhhhhhhhhhhhhhhhhhh