2016-07-14 16 views
0

チャンスは、私はあまりにも疲れていると私は前にスレッド行っているが、私は述べている一般的なエラーに出くわすよ突然のように寝する必要がありますI入力スレッディング機能以前コード、PyQtはマルチスレッドの例

import thread 
import time 
import ystockquote 
import Tkinter as tk 
from threadexample import * 
import sys 
from PyQt4.QtGui import * 



class Window(QtGui.QDialog): 



    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.ui = Ui_Dialog() 
     self.ui.setupUi(self) 


     style = QStyleFactory.create('Cleanlooks') 
     app.setStyle(style) 



if __name__ == "__main__": 

    app = QtGui.QApplication(sys.argv) 
    viewer = Window() 
    viewer.show() 

    sys.exit(app.exec_()) 

とここでは、(start_stream)と呼ばれるスレッド関数となる

import thread 
import time 
import ystockquote 
import Tkinter as tk 
from threadexample import * 
import sys 
from PyQt4.QtGui import * 



class Window(QtGui.QDialog): 



    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.ui = Ui_Dialog() 
     self.ui.setupUi(self) 


     style = QStyleFactory.create('Cleanlooks') 
     app.setStyle(style) 

     QtCore.QObject.connect(self.ui.startbutton, QtCore.SIGNAL('clicked()'),self.start_stream) 

    def start_stream(threadName, delay): 
     while True: 
      footsie = ystockquote.get_price('^FTSE') 
      self.ui.indexlabel.setText(footsie) 

    try: 
     thread.start_new_thread(start_stream, ("Now Streaming", 5,)) 
    except:    
     self.ui.indexlabel.setText("Error") 

    while True: 
     pass 


if __name__ == "__main__": 

    app = QtGui.QApplication(sys.argv) 
    viewer = Window() 
    viewer.show() 

    sys.exit(app.exec_()) 
+0

コード例のインデントをすべて修正してください。 – ekhumoro

答えて

0

問題はここにある:

class Window(QtGui.QDialog): 
    ... 
    def start_stream(threadName, delay): 
     ... 

start_streamselfパラメータが欠落しているので、あなたは、メソッドの中でそれにアクセスしようとすると、selfが定義されていないことになります。

+0

'Window'クラスの本体で定義されているtry/exceptブロックに未定義の' self'参照があります。そして、WTFは、「本当の間:そこをやっている」ということですか? – ekhumoro

+0

私はdef start_stream(self、threadName、delay)に変更しようとしました。エラー:start_stream()は3つの引数(2が与えられています)をとります –

+0

クラス本体の中で直接スレッドを開始しようとしません。 '__init__'またはそれ自身の関数では、クラスが定義されたときにクラス本体が評価されます。その時点ではインスタンスは存在しません。そして、 'thread.start_new_thread'の代わりに新しい[' threading'](https://docs.python.org/2/library/threading.html)モジュールを使うべきです。 – mata