2017-05-04 10 views
0

QPushButtonの色を変更する必要がありますが、エラーが発生しました: "AttributeError:typeオブジェクト 'ProyectoTFM'に 'ui'属性がありません。 私のスレッドから変数にアクセスするのは分かりません。 これは私のコードです:QtスレッドのPythonでボタンの色を変更する

import sys 
import OpenOPC 
import time 
import threading 

from proyectoQt import * 


def actualizarDatosOPC(): 
    while 1: 
     time.sleep(5) 
     if(itemsOPC[15])[1]!=0: 
      #Error on next line 
      ProyectoTFM.ui.AP08Button.setStyleSheet("background-color: red") 
    return 


class ProyectoTFM(QtGui.QMainWindow): 
    def __init__(self,parent=None): 
     QtGui.QMainWindow.__init__(self,parent) 
     self.ui = Ui_MainWindow() 
     self.ui.setupUi(self) 
     self.startTheThread() 
     print('Init') 

    def startTheThread(self): 
     threadQt = threading.Thread(target = actualizarDatosOPC) 
     threadQt.start() 


def clienteOPC(): 
    opc=OpenOPC.client() 
    opc.connect('Kepware.KEPServerEX.V6') 

    global itemsOPC 

    while 1: 
     itemsOPC = opc.read(opc.list('PLC.PLC.TAGS')) 
     time.sleep(5) 
    return 

threads = list() 
threadOPC = threading.Thread(target=clienteOPC) 
threads.append(threadOPC) 
threadOPC.start() 
time.sleep(5) 


if __name__== "__main__": 
    app=QtGui.QApplication(sys.argv) 
    myapp = ProyectoTFM() 
    myapp.show() 
    sys.exit(app.exec_()) 
    threadOPC.__delete() 

私の英語と感謝のために申し訳ありません。

答えて

0

別のスレッドからの表示をメインのものに変更するのは正しくありません。QThreadを使用せずに問題を解決する方法は、ボタンの色を変更するスロットに接続する信号を作成することです。新しいスレッドからシグナルを出すには、パラメータargsを使ってオブジェクトを渡す必要があります。

def actualizarDatosOPC(obj): 
    while 1: 
     time.sleep(5) 
     if(itemsOPC[15])[1]!=0: 
      #Error on next line 
      obj.sendChangeColor.emit() 
    return 


class ProyectoTFM(QtGui.QMainWindow): 
    sendChangeColor = QtCore.pyqtSignal() 
    def __init__(self,parent=None): 
     QtGui.QMainWindow.__init__(self,parent) 
     self.ui = Ui_MainWindow() 
     self.ui.setupUi(self) 
     self.startTheThread() 
     print('Init') 
     self.sendChangeColor.connect(lambda: self.ui.AP08Button.setStyleSheet("background-color: red")) 

    def startTheThread(self): 
     threadQt = threading.Thread(target = actualizarDatosOPC, args=(self,)) 
     threadQt.start() 
0

いくつかのこと:

  1. は、あなたが実際に機能actualizarDatosOPC にUIを渡すことはありませんので、それは存在を知りません。
  2. PyQtの組み込みスレッドツールを使用できない理由はありますか? PyQtを使うつもりならば、全体のフレームワークを買うのが理にかなっているでしょう。方法は、あなたのウィンドウの作品を初期化しながら、さらに How to use QThread correctly in pyqt with moveToThread()?

    :あなたはこのルートを行くことを選択しない場合

    def startTheThread(self): 
        self.threadQt = QThread() 
        d = actualizarDatosOPC(self) 
        d.moveToThread(self.threadQt) 
        self.threadQt.start() 
    
    def actualizarDatosOPC(widget): 
        .... widget.AP08Button.setStyleSheet("background-color: red") 
    

することは、私が良い例があり、このスレッドを見てみたいですあなたはすべてを行う必要が自己参照であるUIで何かを参照したい時はいつでもその後

class ProyectoTFM(QMainWindow, Ui_MainWindow): 
    def __init__(self, parent): 
     # General Init Stuff 
     super(Login, self).__init__(parent) 
     self.setupUi(self) 

._____、:、これはそれを行うには、より標準的な方法です。たとえば、buttonAという名前のボタンがある場合、self.buttonAが適切な参照になります。

編集:他の回答で述べたように 、実際にボタンの色を変更する適切な方法は、その後、ボタンの色を変更することで対応でき、メインスレッドにトリガを発するようになります。

関連する問題