为什么从文件中的导入python文件调用函数后,PyQt应用程序停止响应?

时间:2019-11-11 17:54:39

标签: python import scipy pyqt plotly

我已经在主应用文件夹中的 main .py文件中创建了PyQt5 GUI。在接口文件中,按钮将启动一个名为Calculation的新类(在entry.py文件中),以传递页面上多个输入的值,并在该类中调用startCalculation()方法。在此方法中,将不同的变量传递给导入的python文件中的方法,然后返回这些计算的结果,并传递给另一个python文件中的下一个计算。这些返回值是包含值的数组的形式(对于y轴,然后使用numpy和plotly显示)。

当我运行该应用程序并单击主界面中的按钮时,该应用程序开始加载(在Mac上为彩虹动画),并说它没有响应。类本身不是问题,因为正常的打印测试可以在startCalculation()方法中进行,但是导入文件中的函数会导致这种情况的发生。另外,终端中不会显示任何错误。

以下是PyQt界面文件(主要 .py)中的代码

check the manual that corresponds to your MySQL server version for the right syntax 
to use near ''DESC'' at line 1

这是在外部文件中调用函数的类中的代码

from app import entry

    def startButton(self):
        massaTotale = float(self.lineEdit.text())
        dragCoefficient = float(self.lineEdit_5.text())
        liftCoefficient = float(self.lineEdit_6.text())
        powerAvionics = float(self.lineEdit_3.text())
        powerPayload = float(self.lineEdit_4.text())
        airSpeed = float(self.lineEdit_8.text())
        valoreUnico = float(self.valoreUnicoEdit.text())
        engineEfficiency = float(self.lineEdit_9.text())
        turbolenceCoeff = float(self.lineEdit_10.text())
        dayOfYear = float(self.day_LineEdit.text())
        latitude = float(self.latitude_LineEdit.text())
        sunsetHourAngle = float(self.sunsetHourAngle_LineEdit.text())
        declination = float(self.declination_LineEdit.text())
        newCaluclation = entry.Calculation(massaTotale, dragCoefficient, liftCoefficient, powerAvionics, powerPayload, airSpeed, valoreUnico, engineEfficiency, turbolenceCoeff, dayOfYear, latitude, sunsetHourAngle, declination)
        newCaluclation.startCalculation()

pLevel.py文件中包含以下代码,应返回值数组,以传递到条目“计算”类文件中的第二个函数。

from app.mainFunctions import pLevel

    #constructor method
    def __init__(self, massaTotale, dragCoefficient, liftCoefficient, powerAvionics, powerPayload, airSpeed, valoreUnico, efficiencyEngine, turbolenceCoeff, dayOfYear, latitude, sunsetHourAngle, declination):
        # calculate plevel
        self.totM = massaTotale
        self.vair = airSpeed
        self.cl = liftCoefficient
        self.cd = dragCoefficient
        self.efficiencyEngine = efficiencyEngine
        self.valoreUnico = valoreUnico
        self.powerAvionics = powerAvionics
        self.powerPayload = powerPayload
        self.turbolenceCoeff = turbolenceCoeff
        self.day_of_year = dayOfYear
        self.latitude = latitude
        self.sunset_hour_angle = sunsetHourAngle
        self.declination = declination

    #starting the calculation
    def startCalculation(self):

        self.x_values, self.pLevel_values = pLevel.calculate_pLevel(self.valoreUnico, self.cd, self.cl, self.totM)

        '''
        self.pEngine_values = pEngine.calculate_pEngine(self.x_values, self.pLevel_values, self.efficiencyEngine, self.turbolenceCoeff)
        self.pOut_values = pOut.calculate_pOut(self.x_values, self.pEngine_values, self.powerAvionics, self.powerPayload)
        self.I_loctime = I_loctime.calculate_I_loctime(self.day_of_year, self.latitude, self.sunset_hour_angle, self.declination)
        self.asm_values = area_Solar_Module.calculate_asm(self.x_values, self.pOut_values, self.I_loctime)
        '''

文件的结构如下:

import math
import numpy as np

import plotly as py
import plotly.graph_objs as go
import ipywidgets as widgets
import plotly.io as pio

import sys
sys.dont_write_bytecode = True

py.offline.init_notebook_mode(connected=True)
pio.renderers.default = "browser"

# calculating pLevel
x_values = []
y_values = []

layoutPLevel = go.Layout(
    title="pLevel",
    yaxis=dict(
        title='pLevel'
    ),
    xaxis=dict(
        title='Surface Area Wing'
    )
)

def calculate_pLevel(valoreUnico, cd, cl, totM):
    x_values = []
    count = 0
    while (count < 5):
        x_values.append(count)
        count = count + 0.01

    y_values = []
    iteration = 0
    while (iteration < len(x_values)):
        x_value = x_values[iteration]
        if (x_value == 0):
            y_value = 0
            y_values.append(y_value)
        else:
            if (valoreUnico == 0.0):
                # nessun dato per valoreUnico dato, utilizza i due valori separati
                y_value = firstPart(cd, cl) * math.sqrt(secondPart(x_value, totM))
                y_values.append(y_value)
            else:
                y_value = valoreUnico * \
                math.sqrt(secondPart(x_value, totM))
                y_values.append(y_value)

            iteration = iteration + 1
    else:
        yNpArray = np.array(y_values)
        xNpArray = np.array(x_values)
        tracePLevel = go.Scatter(
            x=xNpArray,
            y=yNpArray,
            mode='lines',
            name='pLevel',
            line=dict(
                shape='spline'
            )
        )
        figPLevel = go.Figure(data=[tracePLevel], layout=layoutPLevel)
        figPLevel.show()
        return x_values, y_values


def firstPart(cd, cl):
    return (cd / cl**(3/2))


def secondPart(x_value, totM):
    return (2*(totM * 9.81)**3) / (1.225 * x_value)

1 个答案:

答案 0 :(得分:0)

pLevel函数中x_values的循环未将一加到迭代中,因此循环永远持续下去。我只是没有注意到我的错误。

不是

 while (iteration < len(x_values)):
    x_value = x_values[iteration]
    if (x_value == 0):
        y_value = 0
        y_values.append(y_value)

应该是

while (iteration < len(x_values)):
    x_value = x_values[iteration]
    if (x_value == 0):
        y_value = 0
        y_values.append(y_value)
        iteration = iteration+1