我正在尝试为用Python编写的信号分析模拟创建一个GUI。为此,我使用AppJar。但是,当我调用生成信号的函数时,会得到一个ValueError,如标题中所示。你知道吗
我已经阅读了stackOverflow上的每一篇ValueError帖子(我可能错过了一篇,但我已经尽力了),所有这些帖子都是关于额外的间距、不能解析为浮点数的字母等。这些似乎都不适用于这里。你知道吗
基本上,我用这个代码调用一个函数来生成我的信号:
signal_axes = app.addPlot("signal", *logic.signal(5, 2), 0, 0, 1)
以及函数本身的相关部分(在文件中)逻辑.py,已导入)
def signal(electrodes, length):
velocity = math.sqrt((3.2e-19 * kinetic_energy) / (mass * 1.66e-27))
frequency = velocity / length
这不是整个函数,所有变量都声明了,未使用的变量稍后在函数中使用。你知道吗
错误特别指向“频率=速度/长度”的行,告诉我:
TypeError: unsupported operand type(s) for /: 'float' and 'str'
当我试图用“float(length)”来修复它时,我得到了一个错误:
ValueError: could not convert string to float:
在StackExchange上的一个答案中,有人建议使用.strip()除去不可见的空格。所以我试着用:
length.strip()
但这给了我以下错误:
AttributeError: 'float' object has no attribute 'strip'
我在这里慢慢陷入疯狂。顺便说一下,下面的代码是独立的:
import numpy as np
kinetic_energy = 9000
mass = 40
length = 2e-2
velocity = np.sqrt((3.2e-19 * kinetic_energy) / (mass * 1.66e-27))
frequency = float(velocity) / float(length)
print(frequency)
有人能看出哪里不对吗?我已经包括了下面所有相关的代码,它不是我的完整文件,但仅此就应该给出一个输出,至少。你知道吗
运行.py
import logic
from appjar import gui
def generate(btn):
app.updatePlot("signal", *logic.signal(app.getEntry("electrodes"), app.getEntry("length")))
showSignalLabels()
def showSignalLabels():
signal_axes.set_xlabel("time (us)")
signal_axes.set_ylabel("amplitude (uV)")
app.refreshPlot("signal")
app = gui()
signal_axes = app.addPlot("signal", *logic.signal(5, 0.02), 0, 0, 1)
app.addLabelEntry("electrodes", 1, 0, 1)
app.addLabelEntry("length", 2, 0, 1)
showSignalLabels()
app.addButton("Generate", generate)
app.go()
逻辑.py
import numpy as np
import math
import colorednoise as cn
steps = 5000
amplitude = 1
offset_code = 0
kinetic_energy = 9000
mass = 40
centered = 1
def signal(electrodes, length):
velocity = math.sqrt((3.2e-19 * kinetic_energy) / (mass * 1.66e-27))
frequency = velocity / length
time = 2 * (electrodes / frequency)
--- irrelevant code ---
return OutputTime, OutputSignal
编辑:这里是完整的回溯。你知道吗
Exception in Tkinter callback
Traceback (most recent call last):
File "E:\Internship IOM\WPy64-3720\python-3.7.2.amd64\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "E:\Internship IOM\PythonScripts\appJar\appjar.py", line 3783, in <lambda>
return lambda *args: funcName(param)
File "E:/Internship IOM/PythonScripts/appJar/testrun.py", line 12, in generate
app.updatePlot("signal", *logic.signal(app.getEntry("electrodes"), app.getEntry("length")))
File "E:\Internship IOM\PythonScripts\appJar\logic.py", line 33, in signal
frequency = velocity / length
TypeError: unsupported operand type(s) for /: 'float' and 'str'
您应该在呼叫站点进行转换,即:
因为否则函数
logic.signal
接收不同的类型(str
和float
)。这就是为什么您会收到另一个关于float
has nostrip
的错误,因为在代码的其他地方您会:给你一个
float
。你知道吗由于您最初的错误是
could not convert string to float
带有明显的空字符串,因此您需要采取额外的措施来防止应用程序中出现空值。您可以使用try ... except
:相关问题 更多 >
编程相关推荐