2013-04-17 17 views
2

我在尝试定义Tkinter gui中的指数函数时遇到了问题。我的Gui由三个组合框组成,我可以选择不同的数字。当我选择所有数字时,它会显示基于指数函数的结果。我的意思是:Tkinter gui中的指数函数

  • 选择NumberA #Combobox 1
  • 选择NumberB #Combobox 2
  • 选择NumberC#Combobox 3

    Result = exp[(-NumberA/NumberB)* NumberC] 
    

我有什么至今如下,但它不起作用:

#Main Selection 
def exponential(*args): 
    try: 
     product.set('%g' %math.exp((float(Num_A.get())/float(Num_B.get())*float(Num_C.get()),2))) 
    except ValueError: 
     pass 

## variables 
NumA = StringVar() 
NumB = StringVar() 
NumC = StringVar() 

product= DoubleVar() 

#Combo boxes, 
#NumA NumB and NumC are similar 
ttk.Label(stepTen, text="Select A):").grid(column =3, row = 0) 
NumA_Select = Combobox(stepTen, values=("0.1", "0.2", "0.3","0.4",),textvariable=Num_OneT) 
NumA_Select.grid(column=4, row=0, columnspan="5", sticky="nswe") 
NumA.trace("w",exponential) 

## display results 
ttk.Label(stepTen, text = "Exponential Dist result:").grid(column = 3, row = 12) 
ttk.Label(stepTen, textvariable=product).grid(column = 4, row = 12) 

#End Code 
root.mainloop() 

非常感谢您提前!

+0

“不起作用”是什么意思?程序崩溃了吗?它是否计算错误的数字?您的计算机是否冻结?另外,您需要检查代码的格式。 –

回答

1

根据您的示例代码,无处设置NumA,NumBNumC为一个值,并且这些变量不与任何小部件关联。另外,您可以创建名称为NumANumBNumC的变量,但在您的功能中,您使用的是Num_A,Num_BNum_C

+0

亲爱的布赖恩,感谢您的及时回应......当我复制时,我犯了一个错误,我把Num_A放在了NumA以及NumB和NumC,而Num_OneT其实是NumA。我为我的错误道歉。谢谢 – Hector

+0

@Hector:问题仍然是你没有设置'NumB'或'NumbC'。我建议在'except'块中打印出错误,而不是忽略它们。这样你会看到价值无法计算的原因。 –