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

亚历山德罗·格雷戈林

我在创建PyQt5 GUI.py文件这是一个主要的应用程序文件夹中。在界面文件中,一个按钮启动了一个名为 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 文件中包含以下代码,并应返回要传递给条目 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
亚历山德罗·格雷戈林

pLevel 函数中 x_values 的循环没有在迭代中添加 1,因此循环一直持续下去。我只是没有注意到我的错误。

而不是

 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

仅在 html 文件中调用函数后响应 init 应用程序

为什么我不能在python中导入我自己的文件夹?

为什么在实现 AppleScript 支持后,我的 macOS 应用程序停止响应默认的 AppleScript 命令?

为什么当我单击提交按钮时,此 PyQt5 发票 GUI 应用程序的“Python 停止工作”?

C#应用程序停止响应通过TCP接收文件

Python:从导入的文件调用函数

从自己的文件中导入Python函数

Python。无法从文件中导入多个函数

仅在Python文件中导入函数

在QFileDialog上选择文件后,PyQt5应用程序关闭

在 python 文件中导入已导入文件

在Android应用程序中导入文本文件?

在NodeJS应用程序中导入文件的问题`

在应用程序中导入本地库和文件

使用TypeScript在节点应用程序中导入JSON文件

无法在反应应用程序中导入多个 CSS 文件

在python中导入多个文件

在python中导入自己的文件

无法在python中导入文件

在 Python 包中导入文件

如何使用 Google 应用程序脚本在 Google 表格中导入带有 xml 响应的 txt 文件?

当整个包都已导入后,为什么要在Python中导入特定的子包?

在python中关闭文件后的函数调用错误

如何在响应式调用上执行PyQt5应用程序

为什么我不能从文件中导入 React 组件?

如果它的文件夹在另一个应用程序中打开,则无法在 python 中导入我自己的模块

链接领域以响应本机后,应用程序不断停止

从单独的python文件调用的PyQt5按钮方法

什么是Cpanel python应用程序上的“应用程序启动文件”选项?