2014-11-20 89 views
0

我正在編寫一個程序,繪製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' 

所以我的問題有三個部分:

  1. 什麼這個消息是什麼意思?

  2. 如何使該程序起作用?

  3. 是「錯誤信息」正確的詞嗎?我們如何稱呼這些信息?

回答

2

關於你的每一個問題:

  1. 的錯誤消息意味着您正在試圖通過一個實例方法(函數)對象來劃分浮動對象。

  2. 因爲getScale類的一個實例方法,則必須調用它是這樣:

    V=epsilon.get()*(math.exp(-r/sigma.get())-(2/sigma.get())**6) 
    #   ^^      ^^    ^^ 
    

    否則,你將與get函數對象本身進行的計算。

  3. 是的,你可以這樣稱呼它。術語「回溯」,通常是指整個誤差輸出:

    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'