为什么我会收到这个错误

时间:2018-01-12 14:59:29

标签: python tkinter

import tkinter as Tk
from tkinter import ttk
import math as m
win=Tk.Tk()
win.title("Propeller Inputs")

inputFrame=ttk.LabelFrame(win,text="Input")
inputFrame.grid(column=0,row=0)

inputlabel=('Water','Lubrication','Power(KW)','Engine speed(rpm)','Gearbox ratio','Shaft strength(N/mm^2)','Bolt strength(N/mm^2)','Number of bolts','PCD of bolts(mm)')
for a in range (0,9):
    ttk.Label(inputFrame,text=inputlabel[a]).grid(column=0, row=a, pady=5)

#Adding a Combobox
water1=Tk.StringVar()
water1=ttk.Combobox(inputFrame, width=20)
water1['values']=("Sea", "River")
water1.grid(column=1, row=0)
water1.current(0)

lub1=Tk.StringVar()
lub1=ttk.Combobox(inputFrame, width=20)
lub1['values']=("Water", "Oil")
lub1.grid(column=1, row=1)
lub1.current(0)

#Adding a Textbox
P1=Tk.StringVar()
P1=ttk.Entry(inputFrame, width=20)
P1.grid(column=1, row=2)

en1=Tk.StringVar()
en1=ttk.Entry(inputFrame, width=20)
en1.grid(column=1, row=3)

gb1=Tk.StringVar()
gb1=ttk.Entry(inputFrame, width=20)
gb1.grid(column=1, row=4)

T1=Tk.StringVar()
T1=ttk.Entry(inputFrame, width=20)
T1.grid(column=1, row=5)

Tb1=Tk.StringVar()
Tb1=ttk.Entry(inputFrame, width=20)
Tb1.grid(column=1, row=6)

i1=Tk.StringVar()
i1=ttk.Entry(inputFrame, width=20)
i1.grid(column=1, row=7)

D1=Tk.StringVar()
D1=ttk.Entry(inputFrame, width=20)
D1.grid(column=1, row=8)




def calculate():   
    P=float(P1.get())

    en=float(en1.get())
    gb=float(gb1.get())
    T=float(T1.get())
#    Tb=float(Tb1.get())
#    i=float(i1.get())
#    D=float(D1.get())
    k2=1.10
    n=m.ceil(en/gb)


    if "Water" in (lub1.get()):
        #print("Water lubricated")
        k1=1.29
    else:
        #print("Oil lubricated")
        k1=1.26
    #print (k1)


    #tailshaft dia, intermediate shaft dia 
    if "Sea" in (water1.get()):
        dp=m.ceil(100*k1*((560*P)/(n*(T+160)))**0.33)
        d=m.ceil(100*k2*((560*P)/(n*(T+160)))**0.33)

    else:
        dp=m.ceil(103.5*k1*((410*P)/(n*(T+160)))**0.33)
        d=m.ceil(103.5*k2*((410*P)/(n*(T+160)))**0.33)

#    return (dp,d)


    update(dp,d)    


def update(dp,d):

    T=float(T1.get())
    Tb=float(Tb1.get())
    i=float(i1.get())
    D=float(D1.get())

    #bolt dia
    if "Sea" in (water1.get()):
        db=m.ceil(0.65*(((d**3.0)*(T+160))/(i*D*Tb))**0.5)
    else:
        db=m.ceil(((0.427*(d**3.0)*(T+155))/(i*D*Tb))**0.5)

    #liner thickness
    t=m.ceil((168+dp)/28) 

    #bush length
    if "Water" in (lub1.get()):
         l=m.ceil(4*dp) 

    else:
         l=m.ceil(2*dp) 

    #c/s area of key
    Ak=m.ceil((d**3)/(2.6*d))
    print(dp,d,db,Ak,l,t)
    #Output  frame
    outputFrame=ttk.LabelFrame(win,text="Output")
    outputFrame.grid(column=2,row=0)

    outputlabel=("Diameter of Tailshaft (mm)=","Diameter of Intermediate shaft (mm) =","Diameter of bolt (mm) =","Thickness of liner (mm) =","Sternbush bearing length (mm) =","Cross-sectional Area of keyway (mm^2) =")
    for a in range (0,6):
        ttk.Label(outputFrame,text=outputlabel[a]).grid(column=0, row=a, padx=20)

    output=(dp,d,db,t,l,Ak)
    for a in range (0,6):
        ttk.Label(outputFrame,text=output[a]).grid(column=1, row=a, padx=20)

    #Adding a button
    enter1=ttk.Button(outputFrame, text="Change values", command=change)
    enter1.grid(column=1, row=10)



def change():

    #Customer Changes Frame
    updateFrame=ttk.LabelFrame(win,text="Changes")
    updateFrame.grid(column=0,row=12)

    changelabel=("Diameter of Tailshaft (mm)","Diameter of Intermediate shaft (mm)")
    for b in range (0,2):
        ttk.Label(updateFrame,text=changelabel[b]).grid(column=0, row=b, pady=5)

    dp=Tk.StringVar()
    dp=ttk.Entry(updateFrame, width=20)
    dp.grid(column=1, row=0)

    d=Tk.StringVar()
    d=ttk.Entry(updateFrame, width=20)
    d.grid(column=1, row=1)
    dp1=dp.get()

    d1=d.get()


    #Adding a button
    enter2=ttk.Button(updateFrame, text="Update", command=lambda:update(dp1,d1))
    enter2.grid(column=1, row=2, pady=10)

    return(dp,d)




#Adding a button
enter=ttk.Button(inputFrame, text="Calculate", command=calculate)
enter.grid(column=1, row=10, pady=5)



win.mainloop()

错误:

1080.0 921.0 1122 326247 4320 45
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Mayur\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/Mayur/Desktop/Program using function calls 01 (single error).py", line 171, in <lambda>
    enter2=ttk.Button(updateFrame, text="Update", command=lambda:update(dp1,d1))
  File "C:/Users/Mayur/Desktop/Program using function calls 01 (single error).py", line 113, in update
    db=m.ceil(0.65*(((d**3.0)*(T+160))/(i*D*Tb))**0.5)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'

i'm not getting where in code string multiplied with float.

1 个答案:

答案 0 :(得分:1)

错误表明问题出在**pow(),行db=m.ceil(0.65*(((d**3.0)*(T+160))/(i*D*Tb))**0.5)

所以问题必须在于本声明中的**运算符  很难看出((d**3.0)*(T+160))/(i*D*Tb))如何产生一个字符串,所以错误必须在这里:d**3.0; d必须是字符串

你的lambda是lambda:update(dp1,d1),早期你有d1=d.get()get()将返回一个字符串,因此您将一个字符串作为第二个参数传递给update(),这会导致您的错误。

d1=d.get()更改为d1=int(d.get())d1=float(d.get())(对非数字案例进行错误检查)应该修复它。