TypeError:不支持的操作数类型为/:'float'。简单的绘图程序
问题描述:
我正在编写一个程序,绘制Lennard-Jones potential,并在GUI中使用滑块调整参数。TypeError:不支持的操作数类型为/:'float'。简单的绘图程序
这是我的代码:
from Tkinter import *
import pylab as p
import math
def show_values():
V=epsilon.get*(math.exp(-r/sigma.get)-(2/sigma.get)**6)
p.plot(r,V)
p.show()
r = p.arange(0.1, 0.2, 0.01)
master = Tk()
epsilon = Scale(master, from_=-10,length=300, to=30, resolution=0.1, width=100)
epsilon.pack()
sigma = Scale(master, from_=-50, to=25, length=300,resolution=0.1, orient=HORIZONTAL)
sigma.pack()
Button(master, text='Show', command=show_values).pack()
mainloop()
但我从我的IDE收到此错误信息(树冠)
%run C:/Users/PC/Desktop/lenard.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.4.1.1975.win-x86\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Users\PC\Desktop\lenard.py", line 7, in show_values
V=epsilon.get*(math.exp(-r/sigma.get)-(2/sigma.get)**6)
TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'
所以我的问题有三个部分:
什么这个消息是什么意思?
如何使该程序起作用?
是“错误信息”正确的词吗?我们如何称呼这些信息?
答
关于你的每一个问题:
的错误消息意味着您正在试图通过一个实例方法(函数)对象来划分浮动对象。
-
因为
get
是Scale
类的一个实例方法,则必须调用它是这样:V=epsilon.get()*(math.exp(-r/sigma.get())-(2/sigma.get())**6) # ^^ ^^ ^^
否则,你将与
get
函数对象本身进行的计算。 -
是的,你可以这样称呼它。术语“回溯”,通常是指整个误差输出:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\PC\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.4.1.1975.win-x86\lib\lib-tk\Tkinter.py", line 1470, in __call__ return self.func(*args) File "C:\Users\PC\Desktop\lenard.py", line 7, in show_values V=epsilon.get*(math.exp(-r/sigma.get)-(2/sigma.get)**6) TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'
而“错误消息”通常是指仅最后一行:
TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'