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

2024-06-28 20:13:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当我运行应用程序并点击主界面上的按钮时,应用程序开始加载(Mac上的彩虹动画),它说它没有响应。当普通的打印测试在startCalculation()方法中工作时,类本身没有问题,但是来自导入文件的函数导致了这种情况。此外,终端中没有给出错误

以下是PyQt接口文件(main.py)中的代码

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()

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

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)
        '''

pLevel.py文件中包含以下代码,应该返回要传递给entry Calculation类文件中的第二个函数的值数组

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)

文件结构如下:

-app
   -- __main__.py
   -- entry.py
   -- __init__.py
   -- mainFunctions
      --- pLevel.py
      --- __init__.py

Tags: 文件textpyimportselfvaluefloatvalues
1条回答
网友
1楼 · 发布于 2024-06-28 20:13:02

pLevel函数中x_值的循环没有将一个值添加到迭代中,因此循环将永远继续。我只是没有注意到我的错误

而不是

 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

相关问题 更多 >