2016-04-14 9 views
1

私はPiで動作するMQTTクライアントへのサブスクリプションに基づいて自動的に更新するQLCDNumberを含むPyQtを使用して構築されたGUIを実行したいと考えています。私は必要なコードの2つのセクションを統合するのに問題があります。私のGUIは以下の通りです。PyQt4とのMQTTの統合

基本的には、トピックの更新/速度の更新時にGUIのLCDも更新したいと思っています。

ありがとうございました!

from PyQt4 import QtGui, QtCore 
import paho.mqtt.client as mqtt 

class Window(QtGui.QMainWindow): 
    def __init__(self): 
     super(Window, self).__init__() 
     self.setGeometry(50,50,500,500) 
     self.setWindowTitle("Think Physics: Technology Wishing Well") 
     self.home() 

    def home(self): 
     mqttLCD = QtGui.QLCDNumber(self) 
     mqttLCD.setNumDigits(1) 
     client.connect('localhost', 1883) 
     self.show() 

    def on_connect(self, client, userdata, rc): 
     print "Connected with result code: " + str(rc) 
     client.subscribe("wishing/speed") 

    def on_message(self, client, userdata, msg): 
     print "Topic: ", msg.topic + '\nMessage: ' + msg.payload 
     mqttLCD.display(msg.payload) 

if __name__ == "__main__": 
    import sys 
    global client = mqtt.Client() 

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


    client.loop_start() 

    sys.exit(app.exec_()) 

答えて

0

client.loop_start()すぐに戻りますが、クライアント操作にとっても非常に重要です。 client.connect()のコールの後に移動すると、正常に動作するはずです。