Я впервые использую переполнение стека, извините, если я что-то не так делаю. Я пытался сделать живой график в окне pyqt5. Программы вроде бы работают, но график ничего не делает, он просто вроде как. График называется MplWidget, он имеет фигуру, холст, сюжет, оси и т. Д. c. Все должно работать, но что-то не так, и я не могу понять, что это такое. Все мои модули обновлены.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import numpy as np
import requests
import math
import psutil
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from itertools import count
from mplwidget import MplWidget
x_vals = []
y_vals = []
index = count()
cpu = psutil.cpu_percent
ram = psutil.virtual_memory()
formatted_data = "Weather couldn't be found"
api_address = "http://api.openweathermap.org/data/2.5/weather?appid=30496dfab732db1b392c099f45264412&q=Valley Stream"
json_data = requests.get(api_address).json()
Min = int(json_data['main']['temp_min'])
Min = (Min - 273.15) * 9/5 + 32
Min = str(math.ceil(Min)) + "°F"
Max = int(json_data['main']['temp_max'])
Max = (Max - 273.15) * 9/5 + 32
Max = str(math.ceil(Max)) + "°F"
humidity = "<br>" + "Humidity: " + str(json_data['main']['humidity'])
formatted_data = json_data['weather'][0]['main'] + "<br>" + json_data['weather'][0]['description'] + "<br>" + "Min: " + Min + "<br>" + "Max: " + Max + humidity
print(formatted_data)
def animate(i):
x_vals.append(next(index))
y_vals.append(psutil.cpu_percent(interval=1, percpu=False))
MplWidget.canvas.axes.clear()
MplWidget.canvas.axes.plot(x_vals, y_vals)
MplWidget.canvas.draw()
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1920, 1080)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(0, 0, 381, 391))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../../../Users/faiqa tauqeer/Desktop/Basically everything you need is in here/Homework Folder/Random pictures/Jarvis_Logo.png"))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(-21, -20, 2151, 1251))
self.label_2.setAutoFillBackground(False)
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap("../../../Users/faiqa tauqeer/Desktop/Basically everything you need is in here/Homework Folder/Random pictures/black screen for text.JPG"))
self.label_2.setObjectName("label_2")
self.calendarWidget = QtWidgets.QCalendarWidget(Form)
self.calendarWidget.setGeometry(QtCore.QRect(1620, 0, 296, 183))
self.calendarWidget.setObjectName("calendarWidget")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(420, 10, 151, 61))
font = QtGui.QFont()
font.setPointSize(30)
self.label_3.setFont(font)
self.label_3.setTextFormat(QtCore.Qt.AutoText)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(10, 400, 191, 71))
font = QtGui.QFont()
font.setPointSize(40)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.button_Google = QtWidgets.QPushButton(Form)
self.button_Google.setEnabled(True)
self.button_Google.setGeometry(QtCore.QRect(1810, 180, 101, 111))
font = QtGui.QFont()
font.setStrikeOut(False)
self.button_Google.setFont(font)
self.button_Google.setAutoFillBackground(False)
self.button_Google.setStyleSheet("qproperty-icon: url(C:/Users/faiqa tauqeer/Desktop/Basically everything you need is in here/Homework Folder/Random pictures/Google_logo.png);\n"
"border: 0px;")
self.button_Google.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../../../Users/faiqa tauqeer/Desktop/Basically everything you need is in here/Homework Folder/Random pictures/Google_logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.button_Google.setIcon(icon)
self.button_Google.setIconSize(QtCore.QSize(100, 100))
self.button_Google.setObjectName("button_Google")
self.youtube_Music = QtWidgets.QPushButton(Form)
self.youtube_Music.setGeometry(QtCore.QRect(1810, 290, 101, 101))
self.youtube_Music.setStyleSheet("border: 0px;")
self.youtube_Music.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("../../../Users/faiqa tauqeer/Desktop/Basically everything you need is in here/Homework Folder/Random pictures/YouTubeMusic_Logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.youtube_Music.setIcon(icon1)
self.youtube_Music.setIconSize(QtCore.QSize(120, 120))
self.youtube_Music.setObjectName("youtube_Music")
self.label_Weather = QtWidgets.QLabel(Form)
self.label_Weather.setGeometry(QtCore.QRect(400, 70, 211, 301))
font = QtGui.QFont()
font.setPointSize(18)
self.label_Weather.setFont(font)
self.label_Weather.setObjectName("label_Weather")
self.label_Emails = QtWidgets.QLabel(Form)
self.label_Emails.setGeometry(QtCore.QRect(10, 470, 291, 601))
self.label_Emails.setObjectName("label_Emails")
self.gridLayoutWidget = QtWidgets.QWidget(Form)
self.gridLayoutWidget.setGeometry(QtCore.QRect(710, 20, 841, 311))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.MplWidget = MplWidget(self.gridLayoutWidget)
self.MplWidget.setObjectName("MplWidget")
self.gridLayout.addWidget(self.MplWidget, 0, 0, 1, 1)
#self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))
self.MplWidget.canvas.axes.clear()
self.MplWidget.canvas.axes.legend(('R.A.M.', 'CPU usage'),loc='upper right')
self.MplWidget.canvas.axes.set_title('System Usage Graph')
#self.MplWidget.canvas.draw()
ani = animation.FuncAnimation(self.MplWidget, animate, interval=1000)
ani
self.label_2.raise_()
self.button_Google.raise_()
self.label.raise_()
self.calendarWidget.raise_()
self.label_3.raise_()
self.label_4.raise_()
self.youtube_Music.raise_()
self.label_Weather.raise_()
self.label_Emails.raise_()
self.gridLayoutWidget.raise_()
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label_3.setText(_translate("Form", "<html><head/><body><p align=\"justify\"><span style=\" color:#0000ff;\">Weather</span></p></body></html>"))
self.label_4.setText(_translate("Form", "<html><head/><body><p align=\"justify\"><span style=\" color:#0000ff;\">E-mails</span></p></body></html>"))
self.label_Weather.setText(_translate("Form", "<html><head/><body><p align=\"justify\"><span style=\" color:#0000ff;\">" + str(formatted_data) + "</span></p></body></html>"))
self.label_Emails.setText(_translate("Form", "<html><head/><body><p align=\"justify\"><span style=\" font-size:18pt; color:#0000ff;\">Emails be like: bruh?</span></p></body></html>"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Это код для MplWidget:
from PyQt5.QtWidgets import QWidget, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MplWidget ( QWidget ):
def __init__ ( self , parent = None ):
QWidget . __init__ ( self , parent )
self . canvas = FigureCanvas ( Figure ())
vertical_layout = QVBoxLayout ()
vertical_layout . addWidget ( self . canvas )
self . canvas . axes = self . canvas . figure . add_subplot ( 111 )
self . setLayout ( vertical_layout )