【发布时间】:2020-05-21 12:05:27
【问题描述】:
我有一个程序,通过单击 PyQT 应用程序上的按钮在另一个窗口中显示 matplotlib 图。 当我单击按钮时,该窗口会显示,但在图上什么也没有发生,在控制台中我得到了这个:
QCoreApplication::exec: 事件循环已经在运行
我有 3 个类和 PyQT5 Gui 文件。 3 类从 TextEdits 获取值,调用第二类,它计算并返回带有 x、y 坐标的列表以在 matplotlib 图上显示(第一类)。我找到了不同的方法来解决这个问题,但是有一些简单案例的例子,我不明白如何将它们应用到我的代码中
from PyQt5 import QtCore, QtGui, QtWidgets
from pythongui4 import Ui_MainWindow
from scipy import integrate
from random import randint
from math import sin, cos, pi
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import sys
import time
# GLOBAL CONSTANTS:
g = 9.80665 # (Gravity acceleration on the Earth), m/s^2
class GraphTrajectoryAnimation(animation.FuncAnimation):
def __init__(self, x, y, color, xlim, ylim, parent=None, m=50, lw=3, width=6, height=4):
"""
Where x, y - NumPy lists with coordinates, color - color of line (default - choose by Random)
height, width, dpi - hw, dpi of the figure from PyLab.
"""
self.figure, self.ax = plt.subplots(figsize=(width, height))
self.ax.set_xlim([0, xlim])
self.ax.set_ylim([0, ylim])
self.graphfunction, = self.ax.plot([], [], color='blue', lw=2)
self.dot, = self.ax.plot([], [], 'o', color='red')
self.x = x
self.y = y
self.m = m
self.color = color
self.lw = lw
def animation(self, i, x, y, m, color):
""" Definition of a matplotlib animation. """
if i * m >= len(x) or i * m >= len(y):
self.graphfunction.set_data(x[:i*m], y[:i*m])
else:
self.graphfunction.set_data(x[:i*m], y[:i*m])
self.dot.set_data(x[i*m], y[i*m])
def graph(self, interval=50):
""" Definition of cycle FuncAnimation, which call animation. """
self.graph_id = animation.FuncAnimation(self.figure, self.animation,
fargs=(self.x, self.y, self.m, self.color),
repeat=False, interval=interval, frames=400)
def showfigure(self):
""" Call the graph function and displays it on the figure. """
self.graph()
plt.show()
class SolveSystemOfADifferentialEquations:
def __init__(self, k, angle, v0, m, ws, tlimit=20):
self.k = k
self.angle = angle
self.m = m
self.ws = ws
self.v0 = v0
self.v0_x = self.v0 * cos(self.angle)
self.v0_y = self.v0 * sin(self.angle)
self.kdivm = self.k / self.m
self.time = np.arange(0, tlimit, 0.0005)
def xmodel(self, X, t):
x = X[0]
dx = X[1]
zdot = [ [], [] ]
zdot[0] = dx
zdot[1] = -self.kdivm * dx + self.ws
return zdot
def ymodel(self, Y, t):
y = Y[0]
dy = Y[1]
zdot = [ [], [] ]
zdot[0] = dy
zdot[1] = -g - self.kdivm * dy
return zdot
def solveX(self):
x = integrate.odeint(self.xmodel, [0, self.v0_x], self.time)
return x
def solveY(self):
y = integrate.odeint(self.ymodel, [0, self.v0_y], self.time)
return y
class MainCommunicationWithGui:
def startbutton(self):
self.getvalues()
def getvalues(self):
ws = float(ui.textEdit_1.toPlainText())
m = float(ui.textEdit_2.toPlainText())
if m == 0:
return False
k = float(ui.textEdit_3.toPlainText())
angle = float(ui.textEdit_4.toPlainText()) * (pi / 180)
v0 = float(ui.textEdit_5.toPlainText())
xlim = float(ui.textEdit_7.toPlainText())
ylim = float(ui.textEdit_8.toPlainText())
x, y = self.tosystem(k, angle, v0, m, ws)
self.figureinit(x, y, xlim, ylim)
def tosystem(self, k, angle, v0, m, ws):
system = SolveSystemOfADifferentialEquations(k, angle, v0, m, ws)
Xi = np.array(system.solveY())
Yi = np.array(system.solveY())
x = []
y = []
for j in range(1, len(Yi)):
if Yi[j][0] > 0:
x.append(Yi[j][0])
y.append(Xi[j][0])
return x, y
def figureinit(self, x, y, xlim, ylim):
colors = ['blue', 'green', 'cyan', 'magenta', 'black']
figure = GraphTrajectoryAnimation(x, y, colors[randint(0, 4)] , xlim, ylim)
figure.showfigure()
def exitbutton(self):
sys.exit(1)
def initbuttons(self):
ui.pushButton_3.clicked.connect(self.startbutton)
ui.pushButton_1.clicked.connect(self.exitbutton)
def main():
global ui, app
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
communicate = MainCommunicationWithGui()
communicate.initbuttons()
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
【问题讨论】: