2011-08-10 9 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) 

おかげ

編集:私は同じ2.7で使用PythonのバージョンよりもチェックしてCMDでプログラムを実行するために使用するコマンドを。: python d:\python\play\speech\speechplay.py

+0

「ランニングパーティー」とはどういう意味ですか?処理されたクリップボードからのテキストの一部に過ぎないのでしょうか? –

+0

それはSTDOUTにテキストを出力しますが、何も言わない ' –

答えて

0

実際、eclipse自体はコマンドラインコマンドを使用してアプリを起動します。

プログラムを起動するには、eclipseがどのコマンドを実行しているかを確認する必要があります。少し冗長かもしれませんが、そこから始めて、必要なものとそうでないものをテストすることができます。

プログラムを実行し、デバッグウィンドウで出力を選択すると、コマンドラインeclipseの使用方法がわかります。右クリックしてプロパティを選択すると、完了です。

デバッグウィンドウがない場合は、ウィンドウ/ show view /(その他の可能性あり)/ debugを開くことができます。

+0

ok eclipseは 'python.exe -u file.py'を使っています。だから私はそれを試みたが、同じ結果を得た –

+0

また、両方のexeとfile.pyのパスは私が推測する?正確に貼り付けるとどうなりますか? – Peter

+0

はい、同じパスです。私は正確にコード化された日食を貼り付けた。 –

2

クリップボードからテキストを読み取るコードに問題がないことを確認しました。

Eclipse以外では存在しないプロジェクトのカスタム環境変数がEclipse設定で指定されているかどうかを確認する必要があります。特に:

  • PYTHONPATH(ともあなたのプログラムは、あなたのセットアップに依存可能性がある上、追加のプロジェクト)を比較するために、あなたのプログラムの冒頭に
  • PATH

使用

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

両方の設定。

その他文体アドバイス:

  • os._exitを使用していないが、

  • (あなたはWindowsでのみ利用できないos.forkに呼び出し、後に子プロセスでos._exitを使用する必要があります)sys.exit好みます

    私はthreading.Eventがより適切であると思うqueue.Queue

  • 私はサブクラスたとえば、内部関数とメソッドを持つスレッドではなく、機能のための小娘アプローチ

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

関連する問題