亲宝软件园·资讯

展开

用pyqt5简单实现Mqtt调试助手-初学python

菁睿物联 人气:1

最近在学习pyqt5,因为我们是做远程物联网设备的,所以就做个mqtt调试助手来练手。第一次做这种程序,没有加异常处理,会有很多不足,欢迎留言拍砖,直接上代码了

这个是程序入口,

 1 # 使用前先安装pyqt5和mqtt包
 2 # 方法:
 3 # pip install PyQt5
 4 # pip install paho-mqtt
 5 
 6 
 7 import sys
 8 import time
 9 
10 from PyQt5.Qt import *
11 from paho.mqtt import client as mqtt
12 from TszsUI import Ui_MainWindow
13 
14 class Window(QMainWindow,Ui_MainWindow):
15     def __init__(self):
16         super().__init__()
17         self.setupUi(self)
18 
19     def mqttlj(self):
20         client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
21         self.client = mqtt.Client(client_id)
22         self.client.username_pw_set(self.Username, self.Password)
23         self.client.on_connect = self.on_connect
24         self.client.on_message = self.on_message
25         self.client.connect(self.HOST, int(self.PORT), 60)
26         # client.loop_forever()  # 用这一句,服务器可以链接上,但是程序会阻塞在这里,使ui处于假死状态,所以换下面一句
27         self.client.loop_start() # 用loop_start()程序会自动开新的线程连接并等待消息的到来
28         print(self.Username, self.Password, self.HOST, self.PORT)
29 
30     def conn_Ck(self):
31         self.Username = self.Name_lineEdit.text()   # 用户名
32         self.Password = self.Pwd_lineEdit.text()    #密码
33         self.HOST = self.Ip_lineEdit.text()       #host 地址
34         self.PORT = self.Port_lineEdit.text()    #端口号
35         self.SubTopic = self.Sub_lineEdit.text() # 订阅的主题
36         self.PubTopic = self.Pub_lineEdit.text() # 发布的主题
37         print(self.PubTopic)
38         self.mqttlj()
39         print("连接")
40 
41     def dk_puBtn(self):
42         self.client.loop_stop()
43         self.Conn_label.setText("连接断开")
44         self.dk_Button.setEnabled(False)
45         self.pushButton.setEnabled(True)
46         print("断开链接")
47 
48     def send_Ck(self):
49         self.payload = self.textEdit.toPlainText()
50         self.on_publish(self.PubTopic,self.payload)
51         print(self.PubTopic)
52         print(self.payload)
53         print("发送")
54 
55     def on_connect(self,client, userdata, flags, rc):
56         if rc == 0:  # rc连接标志,成功会返回0
57             print(rc)
58             self.Conn_label.setText("连接成功")
59             self.dk_Button.setEnabled(True)
60             self.pushButton.setEnabled(False)
61         print("Connected with result code " + str(rc))
62         print(userdata)
63         print(flags)
64         client.subscribe(self.SubTopic)
65 
66     def on_message(self,client, userdata, msg):
67         mm = str(msg.payload).split("'")  # 把接收到的字符串分割 ,分隔符是单引号 ‘
68         missige=mm[1] # 取出收到的消息71         print(msg.topic+" " + ":" + missige) #打印接收的消息
72         self.Rece_text.append(msg.topic+" " + ":" + missige)
73 
74     def on_publish(self,topic,payload,qos = 1):  # 发布消息函数
75         self.client.publish(topic,payload,qos)
76 
77 if __name__ =='__main__':
78     app = QApplication(sys.argv)
79     window = Window()
80 
81     window.show()
82     sys.exit(app.exec_())

 

界面ui实现,是通过pyuic转过来的

  1 # -*- coding: utf-8 -*-
  2 
  3 # Form implementation generated from reading ui file 'Mqtt.ui'
  4 #
  5 # Created by: PyQt5 UI code generator 5.14.0
  6 #
  7 # WARNING! All changes made in this file will be lost!
  8 
  9 
 10 from PyQt5 import QtCore, QtGui, QtWidgets
 11 
 12 
 13 class Ui_MainWindow(object):
 14     def setupUi(self, MainWindow):
 15         MainWindow.setObjectName("MainWindow")
 16         MainWindow.setWindowModality(QtCore.Qt.NonModal)
 17         MainWindow.resize(500, 600)
 18         MainWindow.setMinimumSize(QtCore.QSize(500, 600))
 19         MainWindow.setMaximumSize(QtCore.QSize(500, 600))
 20         self.centralwidget = QtWidgets.QWidget(MainWindow)
 21         self.centralwidget.setObjectName("centralwidget")
 22         self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
 23         self.groupBox.setGeometry(QtCore.QRect(10, 10, 471, 211))
 24         self.groupBox.setObjectName("groupBox")
 25         self.gridLayout = QtWidgets.QGridLayout(self.groupBox)
 26         self.gridLayout.setObjectName("gridLayout")
 27         self.Name_label = QtWidgets.QLabel(self.groupBox)
 28         self.Name_label.setObjectName("Name_label")
 29         self.gridLayout.addWidget(self.Name_label, 0, 0, 1, 1)
 30         self.Name_lineEdit = QtWidgets.QLineEdit(self.groupBox)
 31         self.Name_lineEdit.setObjectName("Name_lineEdit")
 32         self.gridLayout.addWidget(self.Name_lineEdit, 0, 1, 1, 3)
 33         self.Pwd_label = QtWidgets.QLabel(self.groupBox)
 34         self.Pwd_label.setObjectName("Pwd_label")
 35         self.gridLayout.addWidget(self.Pwd_label, 1, 0, 1, 1)
 36         self.Pwd_lineEdit = QtWidgets.QLineEdit(self.groupBox)
 37         self.Pwd_lineEdit.setObjectName("Pwd_lineEdit")
 38         self.gridLayout.addWidget(self.Pwd_lineEdit, 1, 1, 1, 3)
 39         self.IP_label = QtWidgets.QLabel(self.groupBox)
 40         self.IP_label.setObjectName("IP_label")
 41         self.gridLayout.addWidget(self.IP_label, 2, 0, 1, 1)
 42         self.Ip_lineEdit = QtWidgets.QLineEdit(self.groupBox)
 43         self.Ip_lineEdit.setObjectName("Ip_lineEdit")
 44         self.gridLayout.addWidget(self.Ip_lineEdit, 2, 1, 1, 3)
 45         self.Port_label = QtWidgets.QLabel(self.groupBox)
 46         self.Port_label.setObjectName("Port_label")
 47         self.gridLayout.addWidget(self.Port_label, 3, 0, 1, 1)
 48         self.Port_lineEdit = QtWidgets.QLineEdit(self.groupBox)
 49         self.Port_lineEdit.setObjectName("Port_lineEdit")
 50         self.gridLayout.addWidget(self.Port_lineEdit, 3, 1, 1, 3)
 51         self.Sub_label = QtWidgets.QLabel(self.groupBox)
 52         self.Sub_label.setObjectName("Sub_label")
 53         self.gridLayout.addWidget(self.Sub_label, 4, 0, 1, 1)
 54         self.Sub_lineEdit = QtWidgets.QLineEdit(self.groupBox)
 55         self.Sub_lineEdit.setObjectName("Sub_lineEdit")
 56         self.gridLayout.addWidget(self.Sub_lineEdit, 4, 1, 1, 3)
 57         self.Pub_label = QtWidgets.QLabel(self.groupBox)
 58         self.Pub_label.setObjectName("Pub_label")
 59         self.gridLayout.addWidget(self.Pub_label, 5, 0, 1, 1)
 60         self.Pub_lineEdit = QtWidgets.QLineEdit(self.groupBox)
 61         self.Pub_lineEdit.setObjectName("Pub_lineEdit")
 62         self.gridLayout.addWidget(self.Pub_lineEdit, 5, 1, 1, 3)
 63         self.State_label = QtWidgets.QLabel(self.groupBox)
 64         self.State_label.setObjectName("State_label")
 65         self.gridLayout.addWidget(self.State_label, 6, 0, 1, 1)
 66         self.Conn_label = QtWidgets.QLabel(self.groupBox)
 67         self.Conn_label.setStyleSheet("color: rgb(0, 0, 255);")
 68         self.Conn_label.setObjectName("Conn_label")
 69         self.gridLayout.addWidget(self.Conn_label, 6, 1, 1, 1)
 70         self.pushButton = QtWidgets.QPushButton(self.groupBox)
 71         self.pushButton.setObjectName("pushButton")
 72         self.gridLayout.addWidget(self.pushButton, 6, 2, 1, 1)
 73         self.dk_Button = QtWidgets.QPushButton(self.groupBox)
 74         self.dk_Button.setEnabled(False)
 75         self.dk_Button.setObjectName("dk_Button")
 76         self.gridLayout.addWidget(self.dk_Button, 6, 3, 1, 1)
 77         self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
 78         self.groupBox_2.setGeometry(QtCore.QRect(10, 230, 471, 121))
 79         self.groupBox_2.setObjectName("groupBox_2")
 80         self.Rece_text = QtWidgets.QTextEdit(self.groupBox_2)
 81         self.Rece_text.setGeometry(QtCore.QRect(10, 20, 451, 91))
 82         self.Rece_text.setObjectName("Rece_text")
 83         self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
 84         self.groupBox_3.setGeometry(QtCore.QRect(10, 360, 471, 141))
 85         self.groupBox_3.setObjectName("groupBox_3")
 86         self.textEdit = QtWidgets.QTextEdit(self.groupBox_3)
 87         self.textEdit.setGeometry(QtCore.QRect(10, 20, 451, 111))
 88         self.textEdit.setObjectName("textEdit")
 89         self.Send_pushButton = QtWidgets.QPushButton(self.centralwidget)
 90         self.Send_pushButton.setGeometry(QtCore.QRect(330, 520, 141, 23))
 91         self.Send_pushButton.setObjectName("Send_pushButton")
 92         MainWindow.setCentralWidget(self.centralwidget)
 93         self.menubar = QtWidgets.QMenuBar(MainWindow)
 94         self.menubar.setGeometry(QtCore.QRect(0, 0, 500, 23))
 95         self.menubar.setObjectName("menubar")
 96         MainWindow.setMenuBar(self.menubar)
 97         self.statusbar = QtWidgets.QStatusBar(MainWindow)
 98         self.statusbar.setObjectName("statusbar")
 99         MainWindow.setStatusBar(self.statusbar)
100 
101         self.retranslateUi(MainWindow)
102         self.pushButton.clicked.connect(MainWindow.conn_Ck)
103         self.Send_pushButton.clicked.connect(MainWindow.send_Ck)
104         self.dk_Button.clicked.connect(MainWindow.dk_puBtn)
105         QtCore.QMetaObject.connectSlotsByName(MainWindow)
106 
107     def retranslateUi(self, MainWindow):
108         _translate = QtCore.QCoreApplication.translate
109         MainWindow.setWindowTitle(_translate("MainWindow", "MQTT调试助手"))
110         self.groupBox.setTitle(_translate("MainWindow", "连接配置"))
111         self.Name_label.setText(_translate("MainWindow", "用  户  名:"))
112         self.Name_lineEdit.setText(_translate("MainWindow", "name"))
113         self.Pwd_label.setText(_translate("MainWindow", "密      码:"))
114         self.Pwd_lineEdit.setText(_translate("MainWindow", "pass"))
115         self.IP_label.setText(_translate("MainWindow", "IP  地  址:"))
116         self.Ip_lineEdit.setText(_translate("MainWindow", "ip"))
117         self.Port_label.setText(_translate("MainWindow", "端  口  号:"))
118         self.Port_lineEdit.setText(_translate("MainWindow", "1883"))
119         self.Sub_label.setText(_translate("MainWindow", "订阅的主题:"))
120         self.Sub_lineEdit.setText(_translate("MainWindow", "sub/"))
121         self.Pub_label.setText(_translate("MainWindow", "发布的主题:"))
122         self.Pub_lineEdit.setText(_translate("MainWindow", "pub/"))
123         self.State_label.setText(_translate("MainWindow", "连接状态:"))
124         self.Conn_label.setText(_translate("MainWindow", "连接断开"))
125         self.pushButton.setText(_translate("MainWindow", "连接"))
126         self.dk_Button.setText(_translate("MainWindow", "断开"))
127         self.groupBox_2.setTitle(_translate("MainWindow", "数据接收"))
128         self.groupBox_3.setTitle(_translate("MainWindow", "数据发送"))
129         self.textEdit.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
130 "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
131 "p, li { white-space: pre-wrap; }\n"
132 "</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
133 "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'SimSun\';\">qweqwe</span></p></body></html>"))
134         self.Send_pushButton.setText(_translate("MainWindow", "发送"))

加载全部内容

相关教程
猜你喜欢
用户评论