求助为什么绘制不了折线图

m0_71347911 m0_71347911 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ttt.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(761, 440)
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(10, 10, 441, 291))
        self.widget.setObjectName("widget")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(40, 370, 201, 31))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(100, 330, 121, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(50, 330, 51, 16))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(230, 330, 54, 12))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(480, 180, 201, 201))
        self.label_3.setText("")
        self.label_3.setPixmap(QtGui.QPixmap("../图像材料/black_light.jpg"))
        self.label_3.setObjectName("label_3")

        self.label_4 = QtWidgets.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(480, 180, 201, 201))
        self.label_4.setText("")
        self.label_4.setPixmap(QtGui.QPixmap("../图像材料/green_light.jpg"))
        self.label_4.setObjectName("label_4")
        self.label_4.hide()

        self.label_5 = QtWidgets.QLabel(Form)
        self.label_5.setGeometry(QtCore.QRect(480, 180, 201, 201))
        self.label_5.setText("")
        self.label_5.setPixmap(QtGui.QPixmap("../图像材料/red_light.jpg"))
        self.label_5.setObjectName("label_5")
        self.label_5.hide()

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "导出Excle"))
        self.label.setText(_translate("Form", "距离:"))
        self.label_2.setText(_translate("Form", "cm"))

 

# from PyQt5.QtGui import QPixmap
# from threading import Thread
import sys
import time
from collections import deque
import requests
import matplotlib.pyplot as plt
import xlwt
from PyQt5.QtCore import QTimer
from matplotlib import animation
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from matplotlib.figure import Figure

import tui
from PyQt5.QtWidgets import *


class windos(QWidget):

    def __init__(self):
        super(windos, self).__init__()
        self.ui = tui.Ui_Form()
        self.ui.setupUi(self)
        # self.ui.pushButton.clicked.connect(self.exice)
        self.AccessToken()

        self.x = []
        self.y = []

        self.fig, self.axs = plt.subplots(nrows=1, ncols=1, figsize=(10, 5))
        self.canvas = FC(self.fig)
        self.ui.verticalLayout.addWidget(self.canvas)

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.init_ui)
        self.timer.start(1000)  # 每秒更新一次

    def AccessToken(self):
        url = "http://api.nlecloud.com/users/login"
        user = {
            'Account': '1xxxxxxx7',
            'Password': '1xxxx6'
        }
        token = {'AccessToken': requests.post(url=url, data=user).json()['ResultObj']['AccessToken']}
        return token

    def csb_cgq(self):
        while 1:
            self.csb_shuju = requests.get(url=f"http://api.nlecloud.com/devices/1049542/sensors/m_hum",
                                          params=self.AccessToken()).json()['ResultObj']['Value']
            self.csb_shijian = requests.get(url=f"http://api.nlecloud.com/devices/1049542/sensors/m_hum",
                                            params=self.AccessToken()).json()['ResultObj']['RecordTime']

            return self.csb_shuju, self.csb_shijian

    def init_ui(self):
        self.shuju, self.shijian = self.csb_cgq()
        print(self.shuju)
        print(self.shijian)
        self.x.append(self.shijian)
        self.y.append(int(self.shuju))
        if len(self.x) > 5:  # 例如,只保留最近5个数据点
            self.x.pop(0)
            self.y.pop(0)
        print(self.x)
        print(self.y)

        self.axs.clear()
        self.axs.plot(self.x, self.y, label='温度', color='blue', marker='o')
        self.axs.set_title('温度')
        self.axs.set_xlabel('时间')
        self.axs.set_ylabel('温度')
        self.axs.legend()

        # plt.tight_layout()
        # ani = animation.FuncAnimation(self.fig, self.init_ui, interval=1000)  # 每秒更新一次
        # plt.rcParams['font.sans-serif'] = ['SimHei']
        # plt.show()

    def exice(self):
        self.wb = xlwt.Workbook('utf-8')
        self.ws = self.wb.add_sheet('book')
        self.ws.write(0, 0, '数据')
        self.ws.write(0, 1, '时间')
        self.row = 0
        for i in range(20):
            self.row += 1
            self.ws.write(self.row, 0, self.csb_shuju)
            self.ws.write(self.row, 1, self.csb_shijian)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = windos()
    ui.show()
    sys.exit(app.exec_())

 

 

 

 

 

 

...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
m0_71347911
Python 10,221 成员 705 内容