将MQTT与PyQt4集成

时间:2016-04-14 12:32:56

标签: python user-interface pyqt4 mqtt

我正在寻找运行使用PyQt构建的GUI,其中包含QLCDNumber,该QLCDNumber根据对在Pi上运行的MQTT客户端的订阅自动更新。我无法集成我需要的两段代码。我的GUI在下面。

基本上,当主题希望/速度更新时,我希望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_())

1 个答案:

答案 0 :(得分:0)

client.loop_start()立即返回,但对于客户端操作也非常重要。如果您在client.connect()电话之后移动它,那么它应该可以正常工作。