我是python / qt / matplotlib的新手。我使用QtDesigner开始,我已经完成了其他一切工作然后编辑!似乎所有的例子都使用了一种非常不同的方法来调用绘图画布。我正在尝试在QGraphicsView
中获取情节所以在帮助下我能够绘制图表,但是现在当我调用comboevent def时,我遇到了没有错误或发布的崩溃?
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QDate, QDateTime, QTime
from datetime import datetime, timedelta
import random
import matplotlib
import sys
import os
from numpy import arange, sin, pi, to
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class Ui_Form(object):
def setupUi(self, Form):
Graphs = ["None", "Temperature", "Humidity", "Garage Lights", "Path Lights", "HVAC Fan", "HVAC Heat", "HVAC Heat Pump", "HVAC Reverse"]
Graph_Colors= ["Black", "Blue", "Red", "Cyan", "Yellow"]
Form.setObjectName("Form")
Form.setWindowTitle("GraphIt")
Form.resize(805, 585)
self.graphicsView = QtWidgets.QGraphicsView(Form)
self.graphicsView.setGeometry(QtCore.QRect(100, 10, 631, 361))
self.graphicsView.setObjectName("graphicsView")
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
figure = Figure()
self.canvas = FigureCanvas(figure)
self.canvas.setGeometry(0, 0, 500, 500)
lay = QtWidgets.QVBoxLayout(self.graphicsView)
lay.addWidget(self.canvas)
self.axes = figure.gca()
self.axes.set_title("title")
self.axes.plot(t,s)
#figure = Figure()
#self.axes = figure.gca()
#self.set_title("title")
#self.plot(t,s)
#self.canvas = FigureCanvas(figure)
#self.canvas.setGeometry(0, 0, 500, 500)
#lay = QtWidgets.QVBoxLayout(self.graphicsView)
#lay.addWidget(self.canvas)
self.UseCurrentTime = QtWidgets.QRadioButton("Current Time", Form)
self.UseCurrentTime.setGeometry(QtCore.QRect(550, 390, 82, 17))
self.UseCurrentTime.setObjectName("UseCurrentTime")
self.UseCurrentTime.setChecked(True)
self.UseCurrentTime.toggled.connect(self.UseCurrentTimeButton)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(270, 390, 266, 178))
self.widget.setObjectName("widget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.widget)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.StartTime = QtWidgets.QDateTimeEdit(self.widget)
self.StartTime.setCalendarPopup(True)
self.StartTime.setObjectName("StartTime")
self.gridLayout_2.addWidget(self.StartTime, 0, 0, 1, 1)
self.StartTime.setDate(QDate(datetime.now()-timedelta(days=7)))
self.StartTime.setTime(QTime.currentTime())
self.EndTime = QtWidgets.QDateTimeEdit(self.widget)
#self.EndTime = QtWidgets.QDateTimeEdit(self, QDateTime())
print (QDate(datetime.now()-timedelta(days=7)))
self.EndTime.setCalendarPopup(True)
self.EndTime.setCurrentSectionIndex(0)
self.EndTime.setObjectName("EndTime")
self.gridLayout_2.addWidget(self.EndTime, 0, 1, 1, 1)
self.EndTime.setDate(QDate.currentDate())
self.EndTime.setTime(QTime.currentTime())
self.EndTime.setEnabled(False)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setSpacing(12)
self.gridLayout.setObjectName("gridLayout")
#Graph1_Label
self.Graph1_Label = QtWidgets.QLabel("Graph 1", self.widget)
self.Graph1_Label.setAlignment(QtCore.Qt.AlignCenter)
self.Graph1_Label.setObjectName("Graph1_Label")
self.gridLayout.addWidget(self.Graph1_Label, 0, 0, 1, 1)
#Graph1_Select
self.Graph1_Select = QtWidgets.QComboBox(self.widget)
self.Graph1_Select.setObjectName("Graph1_Select")
self.Graph1_Select.addItems(Graphs)
self.gridLayout.addWidget(self.Graph1_Select, 0, 1, 1, 1)
self.Graph1_Select.currentIndexChanged[str].connect(self.onActivated)
#Graph1_Color
self.Graph1_Color = QtWidgets.QComboBox(self.widget)
self.Graph1_Color.setObjectName("Graph1_Color")
self.Graph1_Color.addItems(Graph_Colors)
self.gridLayout.addWidget(self.Graph1_Color, 0, 2, 1, 1)
self.Graph1_Color.currentIndexChanged[str].connect(self.onActivated)
#Graph2_Label
self.Graph_Label2 = QtWidgets.QLabel("Graph 2", self.widget)
self.Graph_Label2.setAlignment(QtCore.Qt.AlignCenter)
self.Graph_Label2.setObjectName("Graph_Label2")
self.gridLayout.addWidget(self.Graph_Label2, 1, 0, 1, 1)
#Graph2_Select
self.Graph2_Select = QtWidgets.QComboBox(self.widget)
self.Graph2_Select.setObjectName("Graph2_Select")
self.Graph2_Select.addItems(Graphs)
self.gridLayout.addWidget(self.Graph2_Select, 1, 1, 1, 1)
self.Graph2_Select.currentIndexChanged[str].connect(self.onActivated)
#Graph2_Color
self.Graph2_Color = QtWidgets.QComboBox(self.widget)
self.Graph2_Color.setObjectName("Graph2_Color")
self.Graph2_Color.addItems(Graph_Colors)
self.gridLayout.addWidget(self.Graph2_Color, 1, 2, 1, 1)
self.Graph2_Color.currentIndexChanged[str].connect(self.onActivated)
#Graph3_Label
self.Graph3_Label = QtWidgets.QLabel("Graph 3", self.widget)
self.Graph3_Label.setAlignment(QtCore.Qt.AlignCenter)
self.Graph3_Label.setObjectName("Graph3_Label")
self.gridLayout.addWidget(self.Graph3_Label, 2, 0, 1, 1)
#Graph3_Select
self.Graph3_Select = QtWidgets.QComboBox(self.widget)
self.Graph3_Select.setObjectName("Graph3_Select")
self.Graph3_Select.addItems(Graphs)
self.gridLayout.addWidget(self.Graph3_Select, 2, 1, 1, 1)
self.Graph3_Select.currentIndexChanged[str].connect(self.onActivated)
#Graph3_Color
self.Graph3_Color = QtWidgets.QComboBox(self.widget)
self.Graph3_Color.setObjectName("Graph3_Color")
self.Graph3_Color.addItems(Graph_Colors)
self.gridLayout.addWidget(self.Graph3_Color, 2, 2, 1, 1)
self.Graph3_Color.currentIndexChanged[str].connect(self.onActivated)
#Graph4_Label
self.Graph4_Label = QtWidgets.QLabel("Graph 4", self.widget)
self.Graph4_Label.setAlignment(QtCore.Qt.AlignCenter)
self.Graph4_Label.setObjectName("Graph4_Label")
self.gridLayout.addWidget(self.Graph4_Label, 3, 0, 1, 1)
#Graph4_Select
self.Graph4_Select = QtWidgets.QComboBox(self.widget)
self.Graph4_Select.setObjectName("Graph4_Select")
self.Graph4_Select.addItems(Graphs)
self.gridLayout.addWidget(self.Graph4_Select, 3, 1, 1, 1)
self.Graph4_Select.currentIndexChanged[str].connect(self.onActivated)
#Graph4_Color
self.Graph4_Color = QtWidgets.QComboBox(self.widget)
self.Graph4_Color.setObjectName("Graph4_Color")
self.Graph4_Color.addItems(Graph_Colors)
self.gridLayout.addWidget(self.Graph4_Color, 3, 2, 1, 1)
self.Graph4_Color.currentIndexChanged[str].connect(self.onActivated)
#Graph5_Label
self.Graph_Label5 = QtWidgets.QLabel("Graph 5", self.widget)
self.Graph_Label5.setAlignment(QtCore.Qt.AlignCenter)
self.Graph_Label5.setObjectName("Graph_Label5")
self.gridLayout.addWidget(self.Graph_Label5, 4, 0, 1, 1)
#Graph5_Select
self.Graph5_Select = QtWidgets.QComboBox(self.widget)
self.Graph5_Select.setObjectName("Graph5_Select")
self.Graph5_Select.addItems(Graphs)
self.gridLayout.addWidget(self.Graph5_Select, 4, 1, 1, 1)
self.Graph5_Select.currentIndexChanged[str].connect(self.onActivated)
#Graph5_Color
self.Graph5_Color = QtWidgets.QComboBox(self.widget)
self.Graph5_Color.setObjectName("Graph5_Color")
self.Graph5_Color.addItems(Graph_Colors)
self.gridLayout.addWidget(self.Graph5_Color, 4, 2, 1, 1)
self.Graph5_Color.currentIndexChanged[str].connect(self.onActivated)
self.gridLayout_2.addLayout(self.gridLayout, 1, 0, 1, 2)
QtCore.QMetaObject.connectSlotsByName(Form)
def onActivated(self, text, axis):
print(text)
self.axes.clear()
t = arange(0.0, 3.0, 0.01)
s = cos(2*pi*t)
self.axes.plot(t,s)
self.canvas.draw()
def UseCurrentTimeButton(self, enabled):
if enabled:
print("enabled")
self.EndTime.setEnabled(False)
else:
print("disabled")
self.EndTime.setEnabled(True)
self.EndTime.setDate(QDate.currentDate())
self.EndTime.setTime(QTime.currentTime())
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_())
答案 0 :(得分:0)
您不必使用QGraphicsView
,您可以使用任何小部件,您需要做的是插入布局并将画布添加到布局中。
[...]
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
figure = Figure()
self.axes = figure.gca()
self..set_title("title")
self..plot(t,s)
self.canvas = FigureCanvas(figure)
self.canvas.setGeometry(0, 0, 500, 500)
lay = QtWidgets.QVBoxLayout(self.graphicsView)
lay.addWidget(self.canvas)
[...]
def onActivated(self, text):
print(text)
self.axes.clear()
t = arange(0.0, 3.0, 0.01)
s = cos(2*pi*t)
self.axes.plot(t,s)
self.canvas.draw()